使用 EventTrigger 设置属性
我希望能够使用 EventTrigger 设置属性,但这有很多问题。
1)EventTriggers仅支持Actions,所以我必须使用storyBoard来设置我的属性。
2) 一旦我使用故事板,我有两个选择:
- 停止:一旦动画停止,值将恢复到动画开始之前的状态
- HoldEnd:这会锁定属性,这样代码和用户交互都无法更改动画开始时的属性。动画正在举行。
在下面的示例中,我想在单击按钮时将 IsChecked 属性设置为 False,并且希望用户能够更改 IsChecked 和/或我希望能够更改代码中的属性。
示例:
<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 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
只需创建您自己的操作即可。
在本例中,我绑定到视图模型上名为 DialogResult 的属性。
Just create your own action.
In this case I'm binding to a property called DialogResult on my viewmodel.
尽管我非常喜欢 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.
我修改了 Neutrino 的解决方案,使 xaml 在指定值时看起来不那么冗长:
抱歉,没有渲染的 xaml 的图片,想象一下您单击的 [=] 汉堡包按钮,它会变成 [<-] 后退按钮,而且切换网格的可见性。
您还可以像 Neutrino 的解决方案中那样指定值:
这是代码。
编辑:Interactivity dll 不再是 Blend 的一部分,现在是“Microsoft.Xaml.Behaviors.Wpf”NuGet 包。 此处列出的代码:https://github.com/microsoft/XamlBehaviorsWpf
请参阅:https://devblogs.microsoft.com/dotnet/open-commerce-xaml- behaviors-for-wpf/
从旧的 Blend Microsoft.Expression.Interactions.dll 迁移到新的开源 Interactivity dll 的步骤(希望我的旧注释是正确的;p):
I modified Neutrino's solution to make the xaml look less verbose when specifying the value:
Sorry for no pictures of the rendered xaml, just imagine a [=] hamburger button that you click and it turns into [<-] a back button and also toggles the visibility of a Grid.
You can also specify values this way like in Neutrino's solution:
And here's the code.
EDIT: The Interactivity dll is no longer part of Blend and is now the "Microsoft.Xaml.Behaviors.Wpf" NuGet package. Code listed here: https://github.com/microsoft/XamlBehaviorsWpf
See: https://devblogs.microsoft.com/dotnet/open-sourcing-xaml-behaviors-for-wpf/
Steps to migrate from old Blend Microsoft.Expression.Interactions.dll to new opensource Interactivity dll (hopefully my old notes are correct ;p):
停止 Storyboard 可以在后面的代码或 xaml 中完成,具体取决于需求的来源。
如果 EventTrigger 移到按钮之外,那么我们可以继续使用另一个 EventTrigger 来定位它,该 EventTrigger 将告诉故事板停止。 当故事板以这种方式停止时,它不会恢复到以前的值。
在这里,我将 Button.Click EventTrigger 移动到周围的 StackPanel,并在 CheckBox.Click 上添加了一个新的 EventTrigger,以便在单击 CheckBox 时停止 Button 的情节提要。 这使我们可以在单击复选框时选中和取消选中该复选框,并为我们提供了所需的按钮取消选中行为。
要停止后面代码中的故事板,我们必须做一些稍微不同的事情。 第三个按钮提供了我们将停止故事板并通过代码将 IsChecked 属性设置回 true 的方法。
我们无法调用 myStoryboard.Stop(),因为我们没有通过设置 isControllable 参数的代码开始 Storyboard。 相反,我们可以删除故事板。 为此,我们需要故事板所在的 FrameworkElement,在本例中是我们的 StackPanel。 删除故事板后,我们可以再次设置 IsChecked 属性,并将其保留到 UI。
Stopping the Storyboard can be done in the code behind, or the xaml, depending on where the need comes from.
If the EventTrigger is moved outside of the button, then we can go ahead and target it with another EventTrigger that will tell the storyboard to stop. When the storyboard is stopped in this manner it will not revert to the previous value.
Here I've moved the Button.Click EventTrigger to a surrounding StackPanel and added a new EventTrigger on the the CheckBox.Click to stop the Button's storyboard when the CheckBox is clicked. This lets us check and uncheck the CheckBox when it is clicked on and gives us the desired unchecking behavior from the button as well.
To stop the storyboard in the code behind, we will have to do something slightly different. The third button provides the method where we will stop the storyboard and set the IsChecked property back to true through code.
We can't call myStoryboard.Stop() because we did not begin the Storyboard through the code setting the isControllable parameter. Instead, we can remove the Storyboard. To do this we need the FrameworkElement that the storyboard exists on, in this case our StackPanel. Once the storyboard is removed, we can once again set the IsChecked property with it persisting to the UI.