Click here to Skip to main content
Licence CPOL
First Posted 24 Sep 2007
Views 124,773
Downloads 2,384
Bookmarked 83 times

A WPF SplitButton

By | 20 Oct 2007 | Article
A Themed WPF SplitButton
Screenshot - SplitButton_a.jpg

Introduction

As the Windows Presentation Foundation (WPF) does not have a split button built-in, I decided to have a go at writing one. As I am fairly new to this Framework, I wanted something simple to start with, and this is what I came up with.

Although it would be possible to create a split button using Styles and Control Templates, I wanted to support the Windows Themes so a custom control seemed like the way to go.

The Control derives from System.Windows.Controls.Button.

Using the Code

To add a split button to a window, reference the assembly, then add an XML namespace to the Windows XAML file as shown below:

xmlns:m="clr-namespace:Wpf.Controls;assembly=Wpf.SplitButton"

The XAML below adds a split button to a window and shows how to add MenuItems that will be displayed by the context menu:

<m:SplitButton Content="Split Button" Placement="Bottom">
    <MenuItem Header="MenuItem 1"/> 
    <MenuItem Header="MenuItem 2"> 
        <MenuItem Header="MenuItem 1"/> 
        <MenuItem Header="MenuItem 2"/> 
    </MenuItem>
</m:SplitButton

The control assembly has a style defined for each of the Windows themes. To draw the control, I've used the ButtonChrome classes from the PresentationFramework DLLs.
The demo project has copies of these styles in the DemoStyles folder so that I could display each theme in the demo window using the x:Key attribute.

I've also added a Style that looks like a Button in Windows Explorer running on Windows Vista. To use this style, you have to set the Style property of the SplitButton explicitly using the following syntax:

Style="{DynamicResource {x:Static m:SplitButtonResources.VistaSplitButtonStyleKey}}" 

Screenshot - SplitButton_vista.jpg

Vista Styled Button with an Icon

Points of Interest

As shown above, the Button includes an Icon property.

To place the context menu, use the Placement, PlacementRectangle, HorizontalOffset and VerticalOffset properties. These are dependency properties defined by the ContextMenuService class, using the AddOwner method. I've added callbacks to each of these properties where I can set the equivalent property on the base Button's internal context menu.

The Button has two modes as defined by the Mode property, Split, which is the default and Dropdown. In Split mode, the control has two parts, the Button part which acts just like a normal button firing the Click event, and a dropdown button which displays the context menu when clicked. In Dropdown mode, clicking anywhere on the button displays the context menu.

Issues

This control was developed using the Beta2 release of Visual Studio 2008. Unfortunately the demo project does not display in the cider designer (for me anyway). This may be a bug in Visual Studio as it displays fine in Expression Blend and compiles and runs without problems.

History

24-Sept-2007: Article posted

02-Oct-2007: Article updated

  • As was discussed in the Icon Thread in the comments below, I've removed the Icon property from the SplitButton as it simply wasn't needed. The demo now includes the preferred method of adding an icon to the button as described by Josh.

09-Oct-2007: Article updated

  • Fixed the error in the demo

20-Oct-2007: Article updated

  • Bug fixes

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

alrh

Software Developer

United Kingdom United Kingdom

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Questionadd child programatically in mvvm Pinmembermtaboy4:08 19 May '12  
QuestionNice control PinmemberMike Hankey9:05 17 Aug '11  
GeneralMy vote of 5 PinmemberMember 47702923:34 25 May '11  
GeneralWow! PinmemberMember 473418722:42 20 Jan '11  
GeneralMy vote of 5 PinmemberMember 473418722:39 20 Jan '11  
GeneralNot displaying correctly PinmemberEspiritu3:55 1 Oct '10  
GeneralRe: Not displaying correctly PinmemberRoeni20:28 15 Jun '11  
QuestionHow to add style besides VistaSplitButtonStyle Pinmemberkairu716:47 26 Mar '10  
Generalexcellent article Pinmemberstg60918:00 5 Mar '10  
General2 databinding related fixes PinmemberMartin Pozor11:38 27 Feb '10  
GeneralExcellent Control (+ 1 issue & fix) [modified] PinmemberMartin Pozor9:17 24 Feb '10  
GeneralRe: Excellent Control (+ 1 issue & fix) Pinmemberchukky416:07 26 Jan '11  
QuestionHow to Set Icon in MenuItem PinmemberFreQi4:05 14 Nov '09  
AnswerRe: How to Set Icon in MenuItem PinmemberFreQi4:32 14 Nov '09  
GeneralMy vote of 1 Pinmemberwest18821:48 27 Oct '09  
GeneralRe: My vote of 1 PinmemberMember 47702923:36 25 May '11  
GeneralThe selected item does not display in the button. Pinmemberwest18821:20 24 Sep '09  
GeneralToolbar question PinmemberDavid Veeneman9:57 11 Sep '09  
QuestionWhat a GREAT control! PinmemberMrPMorris6:24 4 Sep '09  
QuestionHow do I add child MenuItems programatically? Pinmembermraviator4:31 6 Aug '09  
AnswerRe: How do I add child MenuItems programatically? Pinmemberacm_hk21:20 25 Sep '11  
GeneralSplit Button Silverlight PinmemberDavideMar22:28 26 May '09  
GeneralGreat stuff! PinmemberSuper Lloyd14:20 6 Apr '09  
GeneralThis is really awesome, I just have a couple problems Pinmemberssing23:04 10 Mar '09  
GeneralRe: This is really awesome, I just have a couple problems Pinmemberssing11:03 11 Mar '09  
I figured out one way to make it behave as a true dropdown with the left click. Just modify your application's click handler for the button as follows:
 
private void SplitButton_Click(object sender, RoutedEventArgs e)
{
 
if (((SplitButton)sender).Mode == SplitButtonMode.Dropdown)
{
((SplitButton)sender).ContextMenu.PlacementTarget = (SplitButton)sender;
((SplitButton)sender).ContextMenu.IsOpen = true;
 
e.Handled = true;
return;
}

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120529.1 | Last Updated 20 Oct 2007
Article Copyright 2007 by alrh
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid