我有一个WPF风格,这是一个边框。它是用来做按钮的。
<Style x:Key="RoundCorner" TargetType="{x:Type Button}">
<Setter Property="ClickMode" Value="Press"/>
<EventSetter Event="PreviewMouseUp" Handler="RegularButtonRelease"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Grid x:Name="grid">
<Border x:Name="border" CornerRadius="2" BorderBrush="#FF444444" BorderThickness="1">
<Border.Background>
<LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1.2" >
<GradientStop Color="#ffaaaaaa" Offset="0" />
<GradientStop Color="White" Offset="1" />
</LinearGradientBrush>
</Border.Background>
</Border>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsPressed" Value="True">
<!--some style -->
</Trigger>
<Trigger Property="IsMouseOver" Value="True">
<!--some style -->
</Trigger>
<EventTrigger RoutedEvent="PreviewMouseLeftButtonUp">
<BeginStoryboard>
<Storyboard Duration="0:0:2" AutoReverse="False">
<ColorAnimation
Storyboard.TargetProperty="(Border.Background).(SolidColorBrush.Color)"
FillBehavior="Stop" To="Tomato"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
当我释放鼠标单击按钮时,我想在给定的时间内将边框背景色设置为不同的背景色。(例如,按下按钮时是黑色的,当我松开时,它会变成红色,然后又变成白色)
使用上面的代码,我可以看到按钮的颜色在我释放鼠标按钮后一直在变化,我的事件处理程序RegularButtonRelease
也被持续地触发。很快应用程序就会挂起,给我一个System.StackOverflowException
异常。
如果我删除了EventTrigger
的样式,我的应用程序将正确执行,所以我的EventTrigger一定是错的。
我的问题是,如何在鼠标按钮上正确设置背景颜色变化(使用EventTrigger或其他什么)?
更新:
我试图同时设置边界和背景,使用:
<ColorAnimation
Duration="0:0:0.8"
Storyboard.TargetName="border"
Storyboard.TargetProperty="(Border.BorderBrush).(SolidColorBrush.Color)"
To="Red"/>
<ColorAnimation
Duration="0:0:0.8"
Storyboard.TargetName="border"
Storyboard.TargetProperty="(Border.Background).(SolidColorBrush.Color)"
To="Red"/>
这一次边界线变成了红色,工作起来就像一种魅力。但背景仍然在那里,没有任何变化。
我如何才能正确地改变我的背景?
发布于 2016-12-02 07:18:46
或者试试这个例子:
<ControlTemplate.Triggers>
<Trigger Property="IsPressed" Value="True">
<Setter Property="Background" TargetName="Background" Value="Red"/>
</Trigger>
</ControlTemplate.Triggers>
供参考:
Change color of Button when Mouse is over
How do you change Background for a Button MouseOver in WPF?
进入System.StackOverflowException
部分,根据MSDN,您可以通过在堆栈上创建太多的大型对象来获得StackOverflowExceptions
,这通常是因为您的代码已经进入一种状态,它一次又一次地调用相同的函数链。有点像递归。
https://stackoverflow.com/questions/40926570
复制相似问题