这是一个示例样式:
<Style TargetType="Button">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Grid>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal"/>
<VisualState x:Name="Disabled"/>
<VisualState x:Name="MouseOver">
<Storyboard>
<DoubleAnimation Storyboard.TargetName="mouseOverBackgroundImage" Storyboard.TargetProperty="Opacity" Duration="0:0:0.1" To="1"/>
</Storyboard>
</VisualState>
<VisualState x:Name="Pressed">
<Storyboard>
<DoubleAnimation Storyboard.TargetName="pressedBackgroundImage" Storyboard.TargetProperty="Opacity" Duration="0:0:0.1" To="1"/>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Image Name="normalBackgroundImage" Source="{TemplateBinding local:BackgroundImages.NormalBackgroundImage}"/>
<Image Name="mouseOverBackgroundImage" Source="{TemplateBinding local:BackgroundImages.MouseOverBackgroundImage}" Opacity="0"/>
<Image Name="pressedBackgroundImage" Source="{TemplateBinding local:BackgroundImages.PressedBackgroundImage}" Opacity="0"/>
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
在带有附加属性集的 Button 中使用:
<Button local:BackgroundImages.NormalBackgroundImage="C:\Users\Public\Pictures\Sample Pictures\Tulips.jpg"
local:BackgroundImages.MouseOverBackgroundImage="C:\Users\Public\Pictures\Sample Pictures\Desert.jpg"
local:BackgroundImages.PressedBackgroundImage="C:\Users\Public\Pictures\Sample Pictures\Penguins.jpg"
Content="Hello"/>
最后是那些附加属性的定义:
public static class BackgroundImages
{
public static readonly DependencyProperty NormalBackgroundImageProperty =
DependencyProperty.RegisterAttached("NormalBackgroundImage", typeof(ImageSource), typeof(BackgroundImages));
public static readonly DependencyProperty MouseOverBackgroundImageProperty =
DependencyProperty.RegisterAttached("MouseOverBackgroundImage", typeof(ImageSource), typeof(BackgroundImages));
public static readonly DependencyProperty PressedBackgroundImageProperty =
DependencyProperty.RegisterAttached("PressedBackgroundImage", typeof(ImageSource), typeof(BackgroundImages));
public static ImageSource GetNormalBackgroundImage(DependencyObject obj)
{
return (ImageSource)obj.GetValue(NormalBackgroundImageProperty);
}
public static void SetNormalBackgroundImage(DependencyObject obj, ImageSource value)
{
obj.SetValue(NormalBackgroundImageProperty, value);
}
public static ImageSource GetMouseOverBackgroundImage(DependencyObject obj)
{
return (ImageSource)obj.GetValue(MouseOverBackgroundImageProperty);
}
public static void SetMouseOverBackgroundImage(DependencyObject obj, ImageSource value)
{
obj.SetValue(MouseOverBackgroundImageProperty, value);
}
public static ImageSource GetPressedBackgroundImage(DependencyObject obj)
{
return (ImageSource)obj.GetValue(PressedBackgroundImageProperty);
}
public static void SetPressedBackgroundImage(DependencyObject obj, ImageSource value)
{
obj.SetValue(PressedBackgroundImageProperty, value);
}
}
对于触发器,尝试这样的事情。我不确定:(只需将图像添加到项目中的 /Images 文件夹)
<ControlTemplate TargetType="Button">
<Border Name="border" BorderThickness="0"
Background="Transparent">
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background">
<Setter.Value>
<ImageBrush ImageSource="/MyProjectName;component/Images/MyImage.jpg" />
</Setter.Value>
</Setter>
</Trigger>
</ControlTemplate.Triggers>
您可以为 Normal、MouseOver 和 Pressed(可能更多)状态的背景图像定义附加属性。您可以将这些附加属性用于控件模板中单独 Image 控件的 Sourceproperty,并在 VisualState 更改时修改例如 Image 的 Opacity。