使用 EventTrigger 设置属性 [英] Setting a property with an EventTrigger
问题描述
我希望能够使用 EventTrigger 设置属性,这有很多问题.
I want to be able to set a property with an EventTrigger, there's a number of problems with this.
1) EventTriggers 只支持 Actions,所以我必须使用 storyBoard 来设置我的属性.
1) EventTriggers only support Actions, so I must use a storyBoard to set my properties.
2) 一旦我使用故事板,我有两个选择:
2) Once I use a storyboard, I have two options:
- 停止:一旦动画停止,值将恢复到动画开始之前
- HoldEnd:这会锁定属性,因此无论是代码还是用户交互都无法更改动画所持有的属性.
在下面的示例中,我想在单击按钮时将 IsChecked 属性设置为 False,并且我希望用户能够更改 IsChecked 和/或我希望能够在代码中更改该属性.
In the below example, I want to set the IsChecked property to False when the button is clicked and I want the user to be able to change the IsChecked and/or I want to be able to change the property in code.
示例:
<EventTrigger
SourceName="myButton"
RoutedEvent="Button.Click">
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard>
<BooleanAnimationUsingKeyFrames
Storyboard.TargetName="myCheckBox"
Storyboard.TargetProperty="IsChecked"
FillBehavior="Stop">
<DiscreteBooleanKeyFrame
KeyTime="00:00:00"
Value="False" />
</BooleanAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
我意识到我可以在故事板完成后使用Completed"事件将该值设置为 False.但是,在本例中,我希望将逻辑包含在 XAML 中,因为此逻辑将用于自定义控件并且仅特定于 UI.
I realize that I can use the "Completed" event after the storyboard completes to set the value to False. However, in this instance I want to contain the logic within the XAML, as this logic will be used on a custom control and is only specific to the UI.
推荐答案
尽管我喜欢 XAML,但对于此类任务,我转而使用代码隐藏.附加行为 是一个很好的模式.请记住,Expression Blend 3 提供了一种标准方式来编程和使用行为.Expression 社区网站上有一些现有的.
As much as I love XAML, for this kinds of tasks I switch to code behind. Attached behaviors are a good pattern for this. Keep in mind, Expression Blend 3 provides a standard way to program and use behaviors. There are a few existing ones on the Expression Community Site.
这篇关于使用 EventTrigger 设置属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!