5,427,303 members and growing! (19,869 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, C#, Windows, .NET, .NET 3.0, WPF, VS2005, VS2008, Visual Studio, Dev

Posted: 24 Sep 2007
Updated: 20 Oct 2007
Views: 25,328
Bookmarked: 41 times
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
19 votes for this Article.
Popularity: 5.59 Rating: 4.37 out of 5
1 vote, 5.3%
1
1 vote, 5.3%
2
0 votes, 0.0%
3
4 votes, 21.1%
4
13 votes, 68.4%
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



Occupation: Web Developer
Location: United Kingdom United Kingdom

Other popular Windows Presentation Foundation articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 25 of 35 (Total in Forum: 35) (Refresh)FirstPrevNext
Subject  Author Date 
GeneralDropDownButton?memberAlpha Nerd1:29 7 May '08  
GeneralRe: DropDownButton?memberJohn Simmons2:38 7 May '08  
GeneralRe: DropDownButton?memberAlpha Nerd8:20 7 May '08  
QuestionRe: DropDownButton?memberMember 27660918:38 19 Aug '08  
AnswerRe: DropDownButton?memberJohn Simmons0:19 20 Aug '08  
GeneralProblem in placing this control inside a tool barmember BOTs - running in my mind 20:47 1 Apr '08  
QuestionMerging SplitButton into another projectmemberJoe Puccia11:16 30 Jan '08  
QuestionSample dosn't run under Vs2005 and WinXPmemberHorst Klein0:24 23 Nov '07  
AnswerRe: Sample dosn't run under Vs2005 and WinXPmemberalrh1:57 23 Nov '07  
GeneralRe: Sample dosn't run under Vs2005 and WinXPmemberHorst Klein2:05 23 Nov '07  
GeneralRe: Sample dosn't run under Vs2005 and WinXP [modified]memberalrh4:22 23 Nov '07  
GeneralComments?memberTerry7605:10 16 Nov '07  
GeneralBugsmemberCyber Sinh15:19 19 Oct '07  
GeneralRe: Bugsmemberalrh2:03 20 Oct '07  
GeneralWell donememberStabRico4:56 12 Oct '07  
GeneralRe: Well donememberalrh0:03 13 Oct '07  
GeneralError in DemomemberAlan Young2:52 9 Oct '07  
GeneralRe: Error in Demomemberalrh5:57 9 Oct '07  
GeneralNew start upmemberPrathapachandran.v18:21 3 Oct '07  
GeneralRe: New start upmemberalrh22:43 4 Oct '07  
GeneralNice work!memberWindmiller3:53 2 Oct '07  
GeneralRe: Nice work!memberalrh6:27 2 Oct '07  
GeneralBinding the dropdown menu's itemsmvpJosh Smith5:09 1 Oct '07  
GeneralRe: Binding the dropdown menu's itemsmemberalrh8:04 1 Oct '07  
GeneralIcon...memberCromwell3:15 1 Oct '07  

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

PermaLink | Privacy | Terms of Use
Last Updated: 20 Oct 2007
Editor: Deeksha Shenoy
Copyright 2007 by alrh
Everything else Copyright © CodeProject, 1999-2008
Web16 | Advertise on the Code Project