我偶然发现了一个非常烦人的问题,这似乎是一件微不足道的事情,但我无法找到解决方案。我想要做的是将 ColorAnimation 的To属性绑定到名为 MouseOverColor 的 DependencyProperty。这就是我的风格:
<Style TargetType="{x:Type local:Button}">
<Setter Property="Background" Value="{StaticResource SolidBlueDark}" />
<Setter Property="BorderBrush" Value="{x:Null}" />
<Setter Property="BorderThickness" Value="0" />
<Setter Property="FocusVisualStyle" Value="{x:Null}" />
<Setter Property="Foreground" Value="White" />
<Setter Property="HorizontalContentAlignment" Value="Center" />
<Setter Property="MouseOverColor" Value="Cyan" />
<Setter Property="Padding" Value="1" />
<Setter Property="VerticalContentAlignment" Value="Center" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:Button}">
<Grid>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup Name="CommonStates">
<VisualStateGroup.Transitions>
<VisualTransition GeneratedDuration="0:0:0.4" />
</VisualStateGroup.Transitions>
<VisualState Name="Normal" />
<VisualState Name="MouseOver">
<Storyboard>
<ColorAnimation Storyboard.TargetName="Border" Storyboard.TargetProperty="(Border.Background).Color" To="{TemplateBinding MouseOverColor}" />
</Storyboard>
</VisualState>
<VisualState Name="Pressed" />
<VisualState Name="Disabled" />
</VisualStateGroup>
<VisualStateGroup Name="FocusStates">
<VisualState Name="Unfocused" />
<VisualState Name="Focused" />
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Border
Name="Border"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
Height="{TemplateBinding Height}"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
Width="{TemplateBinding Width}" />
<ContentPresenter
TextBlock.Foreground="{TemplateBinding Foreground}"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
Margin="{TemplateBinding Padding}"
RecognizesAccessKey="True"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
发生的事情是什么都没有发生,按钮的颜色保持不变。如果我将 ColorAnimation 线更改为以下内容,它会更改颜色:
<ColorAnimation
Storyboard.TargetName="Border"
Storyboard.TargetProperty="(Border.Background).Color"
To="Cyan" />
但是我需要通过 MouseOverColor 属性设置To颜色而不是硬编码,我该如何实现呢?我尝试了各种绑定,但结果总是一样的。
提前致谢。
编辑:如果我使用静态资源,它也可以工作,但这不是我想要的。真的没有办法做我想做的事吗?为我希望能够使用的每种“鼠标悬停”颜色创建新样式并对其进行硬编码是没有任何意义的。