Click here to Skip to main content
15,884,425 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 309.8K   9.5K   101  
A Themed WPF SplitButton
<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:s="clr-namespace:Wpf.Controls"
    xmlns:luna="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Luna">

	<Style x:Key="ButtonFocusVisual">
		<Setter Property="Control.Template">
			<Setter.Value>
				<ControlTemplate>
					<Rectangle SnapsToDevicePixels="true" Margin="3" Stroke="Black" StrokeDashArray="1 2" StrokeThickness="1"/>
				</ControlTemplate>
			</Setter.Value>
		</Setter>
	</Style>

	<LinearGradientBrush x:Key="ButtonNormalBackground" EndPoint="0,1" StartPoint="0,0">
		<GradientStop Color="#FFFFFFFF" Offset="0"/>
		<GradientStop Color="#FFF3EEDB" Offset="0.9"/>
	</LinearGradientBrush>
	<SolidColorBrush x:Key="ButtonNormalBorder" Color="#FF376206"/>

	<Style x:Key="lunaHomesteadDropDownStyle" TargetType="{x:Type Button}">
		<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
		<Setter Property="Background" Value="{StaticResource ButtonNormalBackground}"/>
		<Setter Property="BorderBrush" Value="{StaticResource ButtonNormalBorder}"/>
		<Setter Property="Template">
			<Setter.Value>
				<ControlTemplate TargetType="{x:Type Button}">
					<Grid Background="Transparent" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
						<luna:ButtonChrome x:Name="chrome" 
							BorderBrush="{TemplateBinding BorderBrush}"
							Fill="{TemplateBinding Background}"
							HorizontalAlignment="Stretch" 
							VerticalAlignment="Stretch"
							RenderMouseOver="{TemplateBinding IsMouseOver}"
							RenderPressed="{TemplateBinding IsPressed}"
							RenderDefaulted="{TemplateBinding IsDefaulted}"
							SnapsToDevicePixels="True">
							<ContentPresenter x:Name="Cp" HorizontalAlignment="Center" VerticalAlignment="Center"/>
						</luna:ButtonChrome>
					</Grid>
					<ControlTemplate.Triggers>
						<Trigger Property="IsEnabled" Value="false">
							<Setter Property="Path.Stroke" TargetName="Cp" Value="#ADADAD"/>
							<Setter Property="Path.Fill" TargetName="Cp" Value="#ADADAD"/>
						</Trigger>
					</ControlTemplate.Triggers>
				</ControlTemplate>
			</Setter.Value>
		</Setter>
	</Style>

	<Style TargetType="{x:Type s:SplitButton}">
		<Setter Property="FocusVisualStyle" Value="{StaticResource ButtonFocusVisual}"/>
		<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
		<Setter Property="Background" Value="{StaticResource ButtonNormalBackground}"/>
		<Setter Property="BorderBrush" Value="{StaticResource ButtonNormalBorder}"/>
		<Setter Property="HorizontalContentAlignment" Value="Center"/>
		<Setter Property="VerticalContentAlignment" Value="Center"/>
		<Setter Property="Padding" Value="0"/>
		<Setter Property="Template">
			<Setter.Value>
				<ControlTemplate TargetType="{x:Type s:SplitButton}">
					<Grid>
						<luna:ButtonChrome x:Name="buttonChrome" 
							HorizontalAlignment="Stretch" 
							VerticalAlignment="Stretch"
							BorderBrush="{TemplateBinding BorderBrush}"
							Fill="{TemplateBinding Background}"
							RenderMouseOver="{TemplateBinding IsMouseOver}"
							RenderPressed="{TemplateBinding IsContextMenuOpen}"
							RenderDefaulted="{TemplateBinding IsDefaulted}"
							SnapsToDevicePixels="True">

							<Grid Background="Transparent" VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
								<Grid.ColumnDefinitions>
									<ColumnDefinition Width="*"/>
									<ColumnDefinition Width="16"/>
								</Grid.ColumnDefinitions>
								
								<Border x:Name="Bd" Margin="1,0,1,0">
									<ContentPresenter
										HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
										VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
										Margin="{TemplateBinding Padding}"
										RecognizesAccessKey="True"
										/>
								</Border>

								<Path x:Name="path"
									Data="M0,0L3,3 6,0z" 
									Margin="5,1,0,0" 
									Grid.Column="1"
									HorizontalAlignment="Center" 
									VerticalAlignment="Center"
									Stroke="{TemplateBinding Foreground}" 
									Fill="{TemplateBinding Foreground}" 
									/>

								<Button x:Name="PART_DropDown"
									Background="{TemplateBinding Background}"
									BorderBrush="{TemplateBinding BorderBrush}"
									Foreground="{TemplateBinding Foreground}"
									VerticalAlignment="Stretch"
									Margin="0,-4,-4,-4"
									Style="{DynamicResource lunaHomesteadDropDownStyle}"
									HorizontalAlignment="Stretch"
									Grid.Column="1"
									Opacity="0">

									<Path Data="M0,0L3,3 6,0z" Margin="1,1,0,0" Grid.Column="1" Stroke="{TemplateBinding Foreground}" Fill="{TemplateBinding Foreground}" />
								</Button>
							</Grid>
						</luna:ButtonChrome>
					</Grid>
					<ControlTemplate.Triggers>
						<Trigger Property="Mode" Value="Dropdown">
							<Setter Property="Visibility" TargetName="PART_DropDown" Value="Collapsed"/>
						</Trigger>
						<Trigger Property="Mode" Value="Button">
							<Setter Property="Visibility" TargetName="PART_DropDown" Value="Collapsed"/>
							<Setter Property="Visibility" TargetName="path" Value="Collapsed"/>
							<Setter Property="Grid.ColumnSpan" TargetName="Bd" Value="2"/>
						</Trigger>
						<Trigger Property="IsMouseOver" SourceName="PART_DropDown" Value="true">
							<Setter Property="Opacity" TargetName="PART_DropDown" Value="1"/>
						</Trigger>
						<Trigger Property="IsEnabled" Value="false">
							<Setter Property="Foreground" Value="#ADADAD"/>
						</Trigger>
					</ControlTemplate.Triggers>
				</ControlTemplate>
			</Setter.Value>
		</Setter>
	</Style>
</ResourceDictionary>

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

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