Click here to Skip to main content
15,879,348 members
Articles / Desktop Programming / WPF

A WPF SplitButton

Rate me:
Please Sign up or sign in to vote.
4.71/5 (44 votes)
20 Oct 2007CPOL2 min read 309.6K   9.5K   101   76
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:

XML
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:

XML
<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:

XML
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)


Written By
Software Developer
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionControl won't show if in same assembly Pin
KyonKyon24-May-18 3:23
KyonKyon24-May-18 3:23 
QuestionContent Property issue Pin
Member 122757717-Jun-16 2:48
Member 122757717-Jun-16 2:48 
PraiseThank you!!!! Pin
dinakl216-Mar-16 23:57
dinakl216-Mar-16 23:57 
Questionevents! Pin
Kalisoft15-Aug-15 7:51
professionalKalisoft15-Aug-15 7:51 
GeneralMy vote of 5 Pin
John Bracey11-Jun-13 22:00
John Bracey11-Jun-13 22:00 
Questionadd child programatically in mvvm Pin
mtaboy19-May-12 4:08
mtaboy19-May-12 4:08 
QuestionNice control Pin
Mike Hankey17-Aug-11 9:05
mveMike Hankey17-Aug-11 9:05 
GeneralMy vote of 5 Pin
Member 477029225-May-11 3:34
Member 477029225-May-11 3:34 
GeneralWow! Pin
Member 473418720-Jan-11 22:42
Member 473418720-Jan-11 22:42 
GeneralMy vote of 5 Pin
Member 473418720-Jan-11 22:39
Member 473418720-Jan-11 22:39 
GeneralNot displaying correctly Pin
Espiritu1-Oct-10 3:55
Espiritu1-Oct-10 3:55 
GeneralRe: Not displaying correctly Pin
Roeni15-Jun-11 20:28
Roeni15-Jun-11 20:28 
QuestionHow to add style besides VistaSplitButtonStyle Pin
kairu726-Mar-10 16:47
kairu726-Mar-10 16:47 
Generalexcellent article Pin
stg6095-Mar-10 18:00
stg6095-Mar-10 18:00 
General2 databinding related fixes Pin
Martin Pozor27-Feb-10 11:38
Martin Pozor27-Feb-10 11:38 
GeneralExcellent Control (+ 1 issue & fix) [modified] Pin
Martin Pozor24-Feb-10 9:17
Martin Pozor24-Feb-10 9:17 
GeneralRe: Excellent Control (+ 1 issue & fix) Pin
chukky426-Jan-11 16:07
chukky426-Jan-11 16:07 
QuestionHow to Set Icon in MenuItem Pin
FreQi14-Nov-09 4:05
FreQi14-Nov-09 4:05 
AnswerRe: How to Set Icon in MenuItem Pin
FreQi14-Nov-09 4:32
FreQi14-Nov-09 4:32 
GeneralMy vote of 1 Pin
west18827-Oct-09 21:48
west18827-Oct-09 21:48 
GeneralRe: My vote of 1 PinPopular
Member 477029225-May-11 3:36
Member 477029225-May-11 3:36 
GeneralThe selected item does not display in the button. Pin
west18824-Sep-09 21:20
west18824-Sep-09 21:20 
GeneralToolbar question Pin
David Veeneman11-Sep-09 9:57
David Veeneman11-Sep-09 9:57 
QuestionWhat a GREAT control! Pin
MrPMorris4-Sep-09 6:24
MrPMorris4-Sep-09 6:24 
QuestionHow do I add child MenuItems programatically? Pin
mraviator6-Aug-09 4:31
mraviator6-Aug-09 4:31 

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

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