Click here to Skip to main content
Click here to Skip to main content

A WPF SplitButton

By , 20 Oct 2007
Rate this:
Please Sign up or sign in to vote.
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
No Biography provided

Comments and Discussions

 
Generalexcellent article Pinmemberstg6095-Mar-10 18:00 
General2 databinding related fixes PinmemberMartin Pozor27-Feb-10 11:38 
GeneralExcellent Control (+ 1 issue & fix) [modified] PinmemberMartin Pozor24-Feb-10 9:17 
GeneralRe: Excellent Control (+ 1 issue & fix) Pinmemberchukky426-Jan-11 16:07 
QuestionHow to Set Icon in MenuItem PinmemberFreQi14-Nov-09 4:05 
AnswerRe: How to Set Icon in MenuItem PinmemberFreQi14-Nov-09 4:32 
GeneralMy vote of 1 Pinmemberwest18827-Oct-09 21:48 
GeneralRe: My vote of 1 PinmemberMember 477029225-May-11 3:36 
GeneralThe selected item does not display in the button. Pinmemberwest18824-Sep-09 21:20 
GeneralToolbar question PinmemberDavid Veeneman11-Sep-09 9:57 
QuestionWhat a GREAT control! PinmemberMrPMorris4-Sep-09 6:24 
QuestionHow do I add child MenuItems programatically? Pinmembermraviator6-Aug-09 4:31 
AnswerRe: How do I add child MenuItems programatically? Pinmemberacm_hk25-Sep-11 21:20 
GeneralSplit Button Silverlight PinmemberDavideMar26-May-09 22:28 
GeneralGreat stuff! PinmemberSuper Lloyd6-Apr-09 14:20 
GeneralThis is really awesome, I just have a couple problems Pinmemberssing10-Mar-09 23:04 
GeneralRe: This is really awesome, I just have a couple problems Pinmemberssing11-Mar-09 11:03 
GeneralRe: This is really awesome, I just have a couple problems Pinmemberalrh12-Mar-09 0:05 
QuestionBug (and suggested fix) found in some of the themes Pinmembermatt3103-Feb-09 9:52 
AnswerRe: Bug (and suggested fix) found in some of the themes Pinmemberalrh12-Mar-09 2:30 
GeneralRe: Bug (and suggested fix) found in some of the themes Pinmembermatt31012-Mar-09 7:23 
GeneralRe: Bug (and suggested fix) found in some of the themes Pinmemberalrh12-Mar-09 8:13 
GeneralRe: Bug (and suggested fix) found in some of the themes Pinmembermatt31023-Mar-09 6:39 
GeneralAdding a Button Mode PinmemberJohnDev21-Jan-09 13:38 
QuestionStyling in a Toolbar PinmemberPaul Auger8-Oct-08 18:51 
QuestionDropDownButton? PinmemberAlpha Nerd7-May-08 0:29 
AnswerRe: DropDownButton? PinmemberJohn Simmons7-May-08 1:38 
GeneralRe: DropDownButton? PinmemberAlpha Nerd7-May-08 7:20 
QuestionRe: DropDownButton? PinmemberMember 276609119-Aug-08 7:38 
AnswerRe: DropDownButton? PinmemberJohn Simmons19-Aug-08 23:19 
AnswerRe: DropDownButton? Pinmemberssing11-Mar-09 11:01 
GeneralProblem in placing this control inside a tool bar Pinmember BOTs - running in my mind 1-Apr-08 19:47 
QuestionMerging SplitButton into another project PinmemberJoe Puccia30-Jan-08 10:16 
AnswerRe: Merging SplitButton into another project Pinmemberxicoloko16-Oct-08 4:24 
QuestionSample dosn't run under Vs2005 and WinXP PinmemberHorst Klein22-Nov-07 23:24 
AnswerRe: Sample dosn't run under Vs2005 and WinXP Pinmemberalrh23-Nov-07 0:57 
GeneralRe: Sample dosn't run under Vs2005 and WinXP PinmemberHorst Klein23-Nov-07 1:05 
GeneralRe: Sample dosn't run under Vs2005 and WinXP [modified] Pinmemberalrh23-Nov-07 3:22 
QuestionComments? PinmemberTerry76016-Nov-07 4:10 
GeneralBugs PinmemberCyber Sinh19-Oct-07 14:19 
GeneralRe: Bugs Pinmemberalrh20-Oct-07 1:03 
GeneralWell done PinmemberStabRico12-Oct-07 3:56 
GeneralRe: Well done Pinmemberalrh12-Oct-07 23:03 
GeneralError in Demo PinmemberAlan Young9-Oct-07 1:52 
GeneralRe: Error in Demo Pinmemberalrh9-Oct-07 4:57 
GeneralNew start up PinmemberPrathapachandran.v3-Oct-07 17:21 
GeneralRe: New start up Pinmemberalrh4-Oct-07 21:43 
GeneralNice work! PinmemberWindmiller2-Oct-07 2:53 
GeneralRe: Nice work! Pinmemberalrh2-Oct-07 5:27 
GeneralBinding the dropdown menu's items PinmvpJosh Smith1-Oct-07 4:09 

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.

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