我正在设计一个自定义按钮,并希望将其背景更改为LinearGradientBrush
on MouseOver
。这是我尝试过的:
<UserControl Height="Auto" Width="Auto">
<UserControl.Resources>
<LinearGradientBrush x:Key="toBackgroundBrush" EndPoint="0,1" StartPoint="0,0">
<GradientStop Color="#FFC8C8C8" Offset="1"/>
<GradientStop Color="#FF626161" Offset="0.599"/>
<GradientStop Color="#FF787878" Offset="0.497"/>
<GradientStop Color="#FF939393" Offset="1"/>
</LinearGradientBrush>
</UserControl.Resources>
<Grid>
<Button Content="Button" Foreground="#FFFFFEFE" Height="30" Width="120">
<Button.Background>
<LinearGradientBrush EndPoint="0,1" StartPoint="0,0">
<GradientStop Color="#FFC8C8C8" Offset="0"/>
<GradientStop Color="#FF626161" Offset="0.539"/>
<GradientStop Color="#FF787878" Offset="0.467"/>
<GradientStop Color="#FF939393" Offset="1"/>
</LinearGradientBrush>
</Button.Background>
<Button.Style>
<Style TargetType="{x:Type Button}">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Trigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<ColorAnimation
Storyboard.TargetProperty="Background"
AutoReverse="True"
Duration="0:0:0.25"
To="{StaticResource toBackgroundBrush}">
</ColorAnimation>
</Storyboard>
</BeginStoryboard>
</Trigger.EnterActions>
</Trigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
</Grid>
然而,当我运行代码时,我得到了这个异常:
Cannot convert the value in attribute 'To' to object of type
'System.Nullable`1[[System.Windows.Media.Color, PresentationCore, Version=3.0.0.0,
Culture=neutral, PublicKeyToken=31bf3856ad364e35]]'.
'System.Windows.Media.LinearGradientBrush' is not a valid value for property 'To'.
我最初尝试MouseOver
在 Blend 中为事件设置动画,但这并没有覆盖默认模板,所以我最终使用情节提要“手动”进行。当真正指向按钮上的属性时,我不明白为什么 aLinearGradientBrush
不是属性的有效值。任何帮助表示赞赏。To
To
Background