Click here to Skip to main content
6,822,123 members and growing! (17,656 online)
Email Password   helpLost your password?
Platforms, Frameworks & Libraries » Windows Presentation Foundation » Controls     Intermediate License: The Code Project Open License (CPOL)

A WPF SplitButton

By alrh

A Themed WPF SplitButton
C#3.0, Windows, .NET3.0, WPF, VS2005, VS2008, Dev
Revision:3 (See All)
Posted:24 Sep 2007
Updated:20 Oct 2007
Views:60,392
Bookmarked:63 times
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
28 votes for this article.
Popularity: 6.25 Rating: 4.32 out of 5
2 votes, 7.1%
1
1 vote, 3.6%
2
1 vote, 3.6%
3
6 votes, 21.4%
4
18 votes, 64.3%
5
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


Member

Occupation: Software Developer
Location: United Kingdom United Kingdom

Other popular Windows Presentation Foundation articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 56 (Total in Forum: 56) (Refresh)FirstPrevNext
GeneralHow to Set Icon in MenuItem PinmemberFreQi5:05 14 Nov '09  
GeneralRe: How to Set Icon in MenuItem PinmemberFreQi5:32 14 Nov '09  
GeneralMy vote of 1 Pinmemberwest18822:48 27 Oct '09  
GeneralThe selected item does not display in the button. Pinmemberwest18822:20 24 Sep '09  
GeneralToolbar question PinmemberDavid Veeneman10:57 11 Sep '09  
GeneralWhat a GREAT control! PinmemberMrPMorris7:24 4 Sep '09  
GeneralHow do I add child MenuItems programatically? Pinmembermraviator5:31 6 Aug '09  
GeneralSplit Button Silverlight PinmemberDavideMar23:28 26 May '09  
GeneralGreat stuff! PinmemberSuper Lloyd15:20 6 Apr '09  
GeneralThis is really awesome, I just have a couple problems Pinmemberssing0:04 11 Mar '09  
GeneralRe: This is really awesome, I just have a couple problems Pinmemberssing12:03 11 Mar '09  
GeneralRe: This is really awesome, I just have a couple problems Pinmemberalrh1:05 12 Mar '09  
QuestionBug (and suggested fix) found in some of the themes Pinmembermatt31010:52 3 Feb '09  
AnswerRe: Bug (and suggested fix) found in some of the themes Pinmemberalrh3:30 12 Mar '09  
GeneralRe: Bug (and suggested fix) found in some of the themes Pinmembermatt3108:23 12 Mar '09  
GeneralRe: Bug (and suggested fix) found in some of the themes Pinmemberalrh9:13 12 Mar '09  
GeneralRe: Bug (and suggested fix) found in some of the themes Pinmembermatt3107:39 23 Mar '09  
GeneralAdding a Button Mode PinmemberJohnDev14:38 21 Jan '09  
QuestionStyling in a Toolbar PinmemberPaul Auger19:51 8 Oct '08  
GeneralDropDownButton? PinmemberAlpha Nerd1:29 7 May '08  
GeneralRe: DropDownButton? PinmemberJohn Simmons2:38 7 May '08  
GeneralRe: DropDownButton? PinmemberAlpha Nerd8:20 7 May '08  
QuestionRe: DropDownButton? PinmemberMember 27660918:38 19 Aug '08  
AnswerRe: DropDownButton? PinmemberJohn Simmons0:19 20 Aug '08  
AnswerRe: DropDownButton? Pinmemberssing12:01 11 Mar '09  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

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

PermaLink | Privacy | Terms of Use
Last Updated: 20 Oct 2007
Editor: Sean Ewington
Copyright 2007 by alrh
Everything else Copyright © CodeProject, 1999-2010
Web20 | Advertise on the Code Project