Click here to Skip to main content
15,891,253 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 310.3K   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

 
AnswerRe: Merging SplitButton into another project Pin
xicoloko16-Oct-08 4:24
xicoloko16-Oct-08 4:24 
QuestionSample dosn't run under Vs2005 and WinXP Pin
Horst Klein22-Nov-07 23:24
Horst Klein22-Nov-07 23:24 
AnswerRe: Sample dosn't run under Vs2005 and WinXP Pin
alrh23-Nov-07 0:57
alrh23-Nov-07 0:57 
GeneralRe: Sample dosn't run under Vs2005 and WinXP Pin
Horst Klein23-Nov-07 1:05
Horst Klein23-Nov-07 1:05 
GeneralRe: Sample dosn't run under Vs2005 and WinXP [modified] Pin
alrh23-Nov-07 3:22
alrh23-Nov-07 3:22 
QuestionComments? Pin
Terry76016-Nov-07 4:10
Terry76016-Nov-07 4:10 
GeneralBugs Pin
Cyber Sinh19-Oct-07 14:19
Cyber Sinh19-Oct-07 14:19 
GeneralRe: Bugs Pin
alrh20-Oct-07 1:03
alrh20-Oct-07 1:03 
Hi Cyber

Cyber Sinh wrote:
1. In the SplitButton.cs (line 137), "s.ContextMenu.HorizontalOffset = (double)e.NewValue;" must be replaced by "s.ContextMenu.VerticalOffset = (double)e.NewValue;"


Doh!!, I'll have to be more careful when using Copy & Paste

Cyber Sinh wrote:
2. In the Vista theme, there is a strange bug. I don't explain it. Here is how to reproduce it:


This was a strange one, I changed a few StaticResource's to DynamicResources which seems to have fixed the problem, (not sure why though?).

I've uploaded an update with the fixes
Let me know if you find anymore.

Thanks again Cyber.
Alan...
GeneralWell done Pin
StabRico12-Oct-07 3:56
StabRico12-Oct-07 3:56 
GeneralRe: Well done Pin
alrh12-Oct-07 23:03
alrh12-Oct-07 23:03 
GeneralError in Demo Pin
Alan Young9-Oct-07 1:52
Alan Young9-Oct-07 1:52 
GeneralRe: Error in Demo Pin
alrh9-Oct-07 4:57
alrh9-Oct-07 4:57 
GeneralNew start up Pin
Prathapachandran3-Oct-07 17:21
professionalPrathapachandran3-Oct-07 17:21 
GeneralRe: New start up Pin
alrh4-Oct-07 21:43
alrh4-Oct-07 21:43 
GeneralNice work! Pin
Windmiller2-Oct-07 2:53
Windmiller2-Oct-07 2:53 
GeneralRe: Nice work! Pin
alrh2-Oct-07 5:27
alrh2-Oct-07 5:27 
GeneralBinding the dropdown menu's items Pin
Josh Smith1-Oct-07 4:09
Josh Smith1-Oct-07 4:09 
GeneralRe: Binding the dropdown menu's items Pin
alrh1-Oct-07 7:04
alrh1-Oct-07 7:04 
GeneralIcon... Pin
Ryan Cromwell1-Oct-07 2:15
Ryan Cromwell1-Oct-07 2:15 
GeneralRe: Icon... Pin
Josh Smith1-Oct-07 5:14
Josh Smith1-Oct-07 5:14 
GeneralRe: Icon... Pin
alrh1-Oct-07 6:50
alrh1-Oct-07 6:50 
GeneralRe: Icon... Pin
Josh Smith1-Oct-07 6:57
Josh Smith1-Oct-07 6:57 
GeneralRe: Icon... Pin
alrh1-Oct-07 7:13
alrh1-Oct-07 7:13 
GeneralRe: Icon... Pin
Josh Smith1-Oct-07 7:46
Josh Smith1-Oct-07 7:46 
GeneralRe: Icon... Pin
alrh1-Oct-07 8:02
alrh1-Oct-07 8:02 

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.