您将需要一个辅助类来附加每个按钮状态的图像源属性和按钮的样式。将帮助程序类放在Helpers
WPF 项目的文件夹中。
助手类
public static class ImageLoader
{
public static ImageSource GetDefaultImage(DependencyObject obj)
{
return (ImageSource)obj.GetValue(DefaultImageProperty);
}
public static void SetDefaultImage(DependencyObject obj, ImageSource value)
{
obj.SetValue(DefaultImageProperty, value);
}
public static readonly DependencyProperty DefaultImageProperty =
DependencyProperty.RegisterAttached(
"DefaultImage",
typeof(ImageSource),
typeof(ImageLoader),
new UIPropertyMetadata(null));
public static ImageSource GetHoverImage(DependencyObject obj)
{
return (ImageSource)obj.GetValue(HoverImageProperty);
}
public static void SetHoverImage(DependencyObject obj, ImageSource value)
{
obj.SetValue(HoverImageProperty, value);
}
public static readonly DependencyProperty HoverImageProperty =
DependencyProperty.RegisterAttached(
"HoverImage",
typeof(ImageSource),
typeof(ImageLoader),
new UIPropertyMetadata(null));
public static ImageSource GetDisabledImage(DependencyObject obj)
{
return (ImageSource)obj.GetValue(DisabledImageProperty);
}
public static void SetDisabledImage(DependencyObject obj, ImageSource value)
{
obj.SetValue(DisabledImageProperty, value);
}
public static readonly DependencyProperty DisabledImageProperty =
DependencyProperty.RegisterAttached(
"DisabledImage",
typeof(ImageSource),
typeof(ImageLoader),
new UIPropertyMetadata(null));
}
按钮样式
<ResourceDictionary ...
xmlns:helper="clr-namespace:MySolution.MyWPFProject.Helpers"
...
>
<Style TargetType="{x:Type Button}" x:Key="MyButtonStyle">
...
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Grid>
<ContentPresenter Name="content"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
Margin="{TemplateBinding Padding}"
RecognizesAccessKey="True"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
/>
<Border>
<!-- Default image -->
<Image Name="image" Source="{TemplateBinding helper:ImageLoader.DefaultImage}" />
</Border>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Cursor" Value="Hand" />
<!-- Hover image -->
<Setter TargetName="image" Property="Source" Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=Button}, Path=(helper:ImageLoader.HoverImage)}" />
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<!-- Disabled image -->
<Setter TargetName="image" Property="Source" Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=Button}, Path=(helper:ImageLoader.DisabledImage)}" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
用法
<UserControl ...
xmlns:helper="clr-namespace:MySolution.MyWPFProject.Helpers"
...
>
<UserControl.Resources >
<ResourceDictionary Source="Path-to-my-button-style.xaml" />
</UserControl.Resources>
...
<Button helper:ImageLoader.DefaultImage="Resources/Images/MyDefaultImage.png"
helper:ImageLoader.HoverImage="Resources/Images/MyHoverImage.png"
helper:ImageLoader.DisabledImage="Resources/Images/MyDisabledImage.png"
Style="{DynamicResource MyButtonStyle}" />
...