使用EventTrigger设置属性 [英] Setting a property with an EventTrigger
问题描述
我希望能够使用EventTrigger设置属性,这有很多问题.
1)EventTriggers仅支持Actions,因此我必须使用StoryBoard设置我的属性.
2)使用情节提要板后,我有两个选择:
- 停止:动画停止播放后,该值将还原为动画开始播放之前的状态
- HoldEnd:这会锁定属性,因此代码和用户交互都无法更改动画持有的属性.
在下面的示例中,我希望在单击按钮时将IsChecked属性设置为False,并且希望用户能够更改IsChecked和/或希望在代码中更改该属性./p>
示例:
<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.
我非常喜欢XAML,因此对于此类任务,我切换到后面的代码. 附加行为是一个很好的模式.请记住,Expression Blend 3 提供了一种编程和使用行为的标准方法. Expression Community网站上有一些现有的./p>
I want to be able to set a property with an EventTrigger, there's a number of problems with this.
1) EventTriggers only support Actions, so I must use a storyBoard to set my properties.
2) Once I use a storyboard, I have two options:
- Stop: Once the animation has stopped the value reverts back to before the animation started
- HoldEnd: This locks the property, so that neither code, nor user interaction can change the property that the animation is holding.
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.
Example:
<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>
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.
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屋!