Click here to Skip to main content
11,920,156 members (59,215 online)
Click here to Skip to main content
Add your own
alternative version


91 bookmarked

A WPF SplitButton

, 20 Oct 2007 CPOL
Rate this:
Please Sign up or sign in to vote.
A Themed WPF SplitButton
Screenshot - SplitButton_a.jpg


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:


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"/> 

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.


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.


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


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


About the Author

Software Developer
United Kingdom United Kingdom
No Biography provided

You may also be interested in...

Comments and Discussions

QuestionHow to add style besides VistaSplitButtonStyle Pin
kairu726-Mar-10 17:47
memberkairu726-Mar-10 17:47 
Generalexcellent article Pin
stg6095-Mar-10 19:00
memberstg6095-Mar-10 19:00 
General2 databinding related fixes Pin
Martin Pozor27-Feb-10 12:38
memberMartin Pozor27-Feb-10 12:38 
GeneralExcellent Control (+ 1 issue & fix) [modified] Pin
Martin Pozor24-Feb-10 10:17
memberMartin Pozor24-Feb-10 10:17 
GeneralRe: Excellent Control (+ 1 issue & fix) Pin
chukky426-Jan-11 17:07
memberchukky426-Jan-11 17:07 
QuestionHow to Set Icon in MenuItem Pin
FreQi14-Nov-09 5:05
memberFreQi14-Nov-09 5:05 
AnswerRe: How to Set Icon in MenuItem Pin
FreQi14-Nov-09 5:32
memberFreQi14-Nov-09 5:32 
GeneralMy vote of 1 Pin
west18827-Oct-09 22:48
memberwest18827-Oct-09 22:48 
GeneralRe: My vote of 1 PinPopular
Member 477029225-May-11 4:36
memberMember 477029225-May-11 4:36 
GeneralThe selected item does not display in the button. Pin
west18824-Sep-09 22:20
memberwest18824-Sep-09 22:20 
GeneralToolbar question Pin
David Veeneman11-Sep-09 10:57
memberDavid Veeneman11-Sep-09 10:57 
QuestionWhat a GREAT control! Pin
MrPMorris4-Sep-09 7:24
memberMrPMorris4-Sep-09 7:24 
QuestionHow do I add child MenuItems programatically? Pin
mraviator6-Aug-09 5:31
membermraviator6-Aug-09 5:31 
AnswerRe: How do I add child MenuItems programatically? Pin
acm_hk25-Sep-11 22:20
memberacm_hk25-Sep-11 22:20 
GeneralSplit Button Silverlight Pin
DavideMar26-May-09 23:28
memberDavideMar26-May-09 23:28 
GeneralGreat stuff! Pin
Super Lloyd6-Apr-09 15:20
memberSuper Lloyd6-Apr-09 15:20 
GeneralThis is really awesome, I just have a couple problems Pin
ssing11-Mar-09 0:04
memberssing11-Mar-09 0:04 
GeneralRe: This is really awesome, I just have a couple problems Pin
ssing11-Mar-09 12:03
memberssing11-Mar-09 12:03 
GeneralRe: This is really awesome, I just have a couple problems Pin
alrh12-Mar-09 1:05
memberalrh12-Mar-09 1:05 
QuestionBug (and suggested fix) found in some of the themes Pin
matt3103-Feb-09 10:52
membermatt3103-Feb-09 10:52 
AnswerRe: Bug (and suggested fix) found in some of the themes Pin
alrh12-Mar-09 3:30
memberalrh12-Mar-09 3:30 
GeneralRe: Bug (and suggested fix) found in some of the themes Pin
matt31012-Mar-09 8:23
membermatt31012-Mar-09 8:23 
GeneralRe: Bug (and suggested fix) found in some of the themes Pin
alrh12-Mar-09 9:13
memberalrh12-Mar-09 9:13 
GeneralRe: Bug (and suggested fix) found in some of the themes Pin
matt31023-Mar-09 7:39
membermatt31023-Mar-09 7:39 
GeneralAdding a Button Mode Pin
JohnDev21-Jan-09 14:38
memberJohnDev21-Jan-09 14:38 
QuestionStyling in a Toolbar Pin
Paul Auger8-Oct-08 19:51
memberPaul Auger8-Oct-08 19:51 
QuestionDropDownButton? Pin
Alpha Nerd7-May-08 1:29
memberAlpha Nerd7-May-08 1:29 
AnswerRe: DropDownButton? Pin
John Simmons7-May-08 2:38
memberJohn Simmons7-May-08 2:38 
GeneralRe: DropDownButton? Pin
Alpha Nerd7-May-08 8:20
memberAlpha Nerd7-May-08 8:20 
QuestionRe: DropDownButton? Pin
Member 276609119-Aug-08 8:38
memberMember 276609119-Aug-08 8:38 
AnswerRe: DropDownButton? Pin
John Simmons20-Aug-08 0:19
memberJohn Simmons20-Aug-08 0:19 
AnswerRe: DropDownButton? Pin
ssing11-Mar-09 12:01
memberssing11-Mar-09 12:01 
GeneralProblem in placing this control inside a tool bar Pin
BOTs - running in my mind 1-Apr-08 20:47
member BOTs - running in my mind 1-Apr-08 20:47 
QuestionMerging SplitButton into another project Pin
Joe Puccia30-Jan-08 11:16
memberJoe Puccia30-Jan-08 11:16 
AnswerRe: Merging SplitButton into another project Pin
xicoloko16-Oct-08 5:24
memberxicoloko16-Oct-08 5:24 
QuestionSample dosn't run under Vs2005 and WinXP Pin
Horst Klein23-Nov-07 0:24
memberHorst Klein23-Nov-07 0:24 
AnswerRe: Sample dosn't run under Vs2005 and WinXP Pin
alrh23-Nov-07 1:57
memberalrh23-Nov-07 1:57 
GeneralRe: Sample dosn't run under Vs2005 and WinXP Pin
Horst Klein23-Nov-07 2:05
memberHorst Klein23-Nov-07 2:05 
GeneralRe: Sample dosn't run under Vs2005 and WinXP [modified] Pin
alrh23-Nov-07 4:22
memberalrh23-Nov-07 4:22 
QuestionComments? Pin
Terry76016-Nov-07 5:10
memberTerry76016-Nov-07 5:10 
GeneralBugs Pin
Cyber Sinh19-Oct-07 15:19
memberCyber Sinh19-Oct-07 15:19 
GeneralRe: Bugs Pin
alrh20-Oct-07 2:03
memberalrh20-Oct-07 2:03 
GeneralWell done Pin
StabRico12-Oct-07 4:56
memberStabRico12-Oct-07 4:56 
GeneralRe: Well done Pin
alrh13-Oct-07 0:03
memberalrh13-Oct-07 0:03 
GeneralError in Demo Pin
Alan Young9-Oct-07 2:52
memberAlan Young9-Oct-07 2:52 
GeneralRe: Error in Demo Pin
alrh9-Oct-07 5:57
memberalrh9-Oct-07 5:57 
GeneralNew start up Pin
Prathapachandran.v3-Oct-07 18:21
memberPrathapachandran.v3-Oct-07 18:21 
GeneralRe: New start up Pin
alrh4-Oct-07 22:43
memberalrh4-Oct-07 22:43 
GeneralNice work! Pin
Windmiller2-Oct-07 3:53
memberWindmiller2-Oct-07 3:53 
GeneralRe: Nice work! Pin
alrh2-Oct-07 6:27
memberalrh2-Oct-07 6:27 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.151120.1 | Last Updated 20 Oct 2007
Article Copyright 2007 by alrh
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid