WPF ToggleButton 自定义控件 并设置背景图片
public class ToggleBrushButton :ToggleButton { public ImageSource CheckedImage { get { return (ImageSource)GetValue(CheckedImageProperty); } set { SetValue(CheckedImageProperty, value); } } // Using a DependencyProperty as the backing store for MyProperty. This enables animation, styling, binding, etc... public static readonly DependencyProperty CheckedImageProperty = DependencyProperty.Register("CheckedImage", typeof(ImageSource), typeof(ToggleBrushButton), new PropertyMetadata((ImageSource)null)); public ImageSource UnCheckedImage { get { return (ImageSource)GetValue(UnCheckedImageProperty); } set { SetValue(UnCheckedImageProperty, value); } } // Using a DependencyProperty as the backing store for UnCheckedImage. This enables animation, styling, binding, etc... public static readonly DependencyProperty UnCheckedImageProperty = DependencyProperty.Register("UnCheckedImage", typeof(ImageSource), typeof(ToggleBrushButton), new PropertyMetadata((ImageSource)null)); public ImageSource CheckedMouseDownImage { get { return (ImageSource)GetValue(CheckedMouseDownImageProperty); } set { SetValue(CheckedMouseDownImageProperty, value); } } // Using a DependencyProperty as the backing store for MyProperty. This enables animation, styling, binding, etc... public static readonly DependencyProperty CheckedMouseDownImageProperty = DependencyProperty.Register("CheckedMouseDownImage", typeof(ImageSource), typeof(ToggleBrushButton), new PropertyMetadata(default(ImageSource))); public ImageSource UnCheckedMouseDownImage { get { return (ImageSource)GetValue(UnCheckedMouseDownImageProperty); } set { SetValue(UnCheckedMouseDownImageProperty, value); } } // Using a DependencyProperty as the backing store for UnCheckedImage. This enables animation, styling, binding, etc... public static readonly DependencyProperty UnCheckedMouseDownImageProperty = DependencyProperty.Register("UnCheckedMouseDownImage", typeof(ImageSource), typeof(ToggleBrushButton), new PropertyMetadata(default(ImageSource))); public string CheckedContent { get { return (string)GetValue(CheckedContentProperty); } set { SetValue(CheckedContentProperty, value); } } // Using a DependencyProperty as the backing store for CheckedContent. This enables animation, styling, binding, etc... public static readonly DependencyProperty CheckedContentProperty = DependencyProperty.Register("CheckedContent", typeof(string), typeof(ToggleBrushButton), new PropertyMetadata("")); public string UnCheckedContent { get { return (string)GetValue(UnCheckedContentProperty); } set { SetValue(UnCheckedContentProperty, value); } } // Using a DependencyProperty as the backing store for UnCheckedContent. This enables animation, styling, binding, etc... public static readonly DependencyProperty UnCheckedContentProperty = DependencyProperty.Register("UnCheckedContent", typeof(string), typeof(ToggleBrushButton), new PropertyMetadata("")); }
<Style x:Key="ToggleImgButtonStyle" TargetType="{x:Type ToggleButton}"> <Setter Property="FocusVisualStyle"> <Setter.Value> <Style> <Setter Property="Control.Template"> <Setter.Value> <ControlTemplate> <Rectangle Margin="2" SnapsToDevicePixels="True" Stroke="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" StrokeThickness="1" StrokeDashArray="1 2"/> </ControlTemplate> </Setter.Value> </Setter> </Style> </Setter.Value> </Setter> <Setter Property="Background" Value="#FFDDDDDD"/> <Setter Property="BorderBrush" Value="#FF707070"/> <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/> <Setter Property="BorderThickness" Value="1"/> <Setter Property="HorizontalContentAlignment" Value="Center"/> <Setter Property="VerticalContentAlignment" Value="Center"/> <Setter Property="Padding" Value="1"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type ToggleButton}"> <Border x:Name="border" Background="{TemplateBinding Background}" SnapsToDevicePixels="True"> <ContentPresenter x:Name="contentPresenter" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" ContentStringFormat="{TemplateBinding ContentStringFormat}" Focusable="False" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/> </Border> <ControlTemplate.Triggers> <Trigger Property="Button.IsDefaulted" Value="True"> <Setter Property="BorderBrush" TargetName="border" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/> </Trigger> <Trigger Property="IsMouseOver" Value="True"> <Setter Property="Background" TargetName="border" Value="#FFBEE6FD"/> <Setter Property="BorderBrush" TargetName="border" Value="#FF3C7FB1"/> </Trigger> <Trigger Property="IsPressed" Value="True"> <Setter Property="Background" TargetName="border" Value="#FFC4E5F6"/> <Setter Property="BorderBrush" TargetName="border" Value="#FF2C628B"/> </Trigger> <Trigger Property="IsChecked" Value="True"> <Setter Property="Background" TargetName="border"> <Setter.Value> <ImageBrush ImageSource="{Binding CheckedImage, RelativeSource={RelativeSource TemplatedParent}}" Stretch="Uniform" /> </Setter.Value> </Setter> </Trigger> <Trigger Property="IsChecked" Value="False"> <Setter Property="Background" TargetName="border"> <Setter.Value> <ImageBrush ImageSource="{Binding UnCheckedImage, RelativeSource={RelativeSource TemplatedParent}}" Stretch="Uniform" /> </Setter.Value> </Setter> </Trigger> <!--<Trigger Property="IsEnabled" Value="True"> <Setter Property="Background" TargetName="border"> <Setter.Value> <ImageBrush ImageSource="{Binding UnCheckedImage, RelativeSource={RelativeSource TemplatedParent}}"/> </Setter.Value> </Setter> </Trigger>--> <Trigger Property="IsEnabled" Value="False"> <Setter Property="Background" TargetName="border"> <Setter.Value> <ImageBrush ImageSource="{Binding CheckedImage, RelativeSource={RelativeSource TemplatedParent}}" Stretch="None"/> </Setter.Value> </Setter> </Trigger> <!--<Trigger Property="IsEnabled" Value="False"> <Setter Property="Background" TargetName="border" Value="#FFF4F4F4"/> <Setter Property="BorderBrush" TargetName="border" Value="#FFADB2B5"/> <Setter Property="TextElement.Foreground" TargetName="contentPresenter" Value="#FF838383"/> </Trigger>--> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style>
<Uc:ToggleBrushButton Style="{StaticResource ToggleImgButtonStyle}"
UnCheckedImage="" UnCheckedMouseDownImage="" CheckedMouseDownImage="" CheckedImage="" />