如何在样式中正确设置 WPF EventTrigger?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40926570/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to correctly set an WPF EventTrigger in style?
提问by Dia
I have a WPF style that is a border. It is used for a button.
我有一个 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>
I want to set the border background color to a different one for a given time, when I release the mouse click on the button. (e.g. the button is black when pressed, and when I release, it changes to red, then changes back to white)
当我松开鼠标单击按钮时,我想在给定时间内将边框背景颜色设置为不同的颜色。(例如按下按钮时为黑色,当我松开时,它变为红色,然后变回白色)
Using the above code, I can see the button color keep changing after I release mouse button, and my event handler RegularButtonReleaseis fired continuously too.
Soon the appplication hangs, and gives me a System.StackOverflowExceptionexception.
使用上面的代码,我可以看到释放鼠标按钮后按钮颜色不断变化,并且我的事件处理程序RegularButtonRelease也不断被触发。很快应用程序挂起,并给了我一个System.StackOverflowException例外。
If I take away the EventTriggerin style, my applications perform correctly, so my EventTrigger must be wrong.
如果EventTrigger我去掉 in 样式,我的应用程序会正确执行,所以我的 EventTrigger 一定是错误的。
My question is, how could I correctly set a background color change at mouse button up (using the EventTrigger or something else)?
我的问题是,如何正确设置鼠标按钮时的背景颜色变化(使用 EventTrigger 或其他东西)?
UPDATE:
更新:
I try to set the border and background at the same time using:
我尝试使用以下方法同时设置边框和背景:
<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"/>
This time the border line is changing to red, works like a charm. But the background still sits there, no any changes.
这次边界线变成了红色,就像一个魅力。但是背景仍然坐在那里,没有任何变化。
How can I correctly change my background?
如何正确更改我的背景?
回答by AnjumSKhan
First, your mistakes :
You are trying to change Colorof Backgroundwhich is not possible as it is set to LinearGradientBrush, and secondly you have not set Storyboard.TargetNameat all.
首先,你的错误:您正在尝试改变Color的Background,因为它被设置成这是不可能的LinearGradientBrush,其次你还没有设置Storyboard.TargetName的。
I have done some changes, first : Assign x:Nameto second GradientStop, and then use this x:Nameas Storyboard.TargetName="C2"in animation.
我也做了一些改变,第一:分配x:Name到第二个GradientStop,然后用这个x:Name作为Storyboard.TargetName="C2"动画。
<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 x:Name="C2" 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:1" AutoReverse="False">
<ColorAnimation Storyboard.TargetName="C2"
Storyboard.TargetProperty="Color"
FillBehavior="Stop" To="Red"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
回答by Ashish Srivastava
Or try this example :
或者试试这个例子:
<ControlTemplate.Triggers>
<Trigger Property="IsPressed" Value="True">
<Setter Property="Background" TargetName="Background" Value="Red"/>
</Trigger>
</ControlTemplate.Triggers>
For reference:
以供参考:
Change color of Button when Mouse is over
How do you change Background for a Button MouseOver in WPF?
如何在 WPF 中更改 Button MouseOver 的背景?
Coming to the System.StackOverflowExceptionpart, according to MSDN you can get StackOverflowExceptionsby creating too many large objects on the stack, it usually happens because your code has got into a state where it is calling the same chain of functions over and over again. Something like recursion.
说到这System.StackOverflowException部分,根据 MSDN,您可以StackOverflowExceptions通过在堆栈上创建太多大对象来获得,这通常是因为您的代码已进入一种状态,它一遍又一遍地调用相同的函数链。类似递归的东西。

