这是场景:
我有以下用户控件,想法是它的 View 模型应该能够向 View 发出信号,它需要“激活发光”,从而播放 Storyboard。
<UserControl x:Class="View.UnitView" ... >
...
<Storyboard x:Key="ActivateGlow">
...
</Storyboard>
...
<!-- INVALID BINDING! Not Dependancy Object-->
<EventTrigger RoutedEvent="{Binding OnActivateGlow}">
<BeginStoryboard Storyboard="{StaticResource ActivateGlow}"/>
</EventTrigger>
</UserControl>
在 UnitView 的代码隐藏中,我有:
public event EventHandler ActivateGlow;
在 MVVM 中很正常,我有以下用于 UnitViewModel 的 DataTemplate:
<DataTemplate DataType="{x:Type vm:UnitViewModel}">
<vw:UnitView d:DesignWidth="150" d:DesignHeight="100" />
</DataTemplate>
最终的问题是,我如何设置一些东西以便 View 模型可以触发 OnActivateGlow 事件?
最佳答案
更新:Firoso,如评论中所述,您应该能够(我认为 - 即未经测试)能够使用混合行为组件来满足您的要求。
除了下载和安装SDK。获取表达式混合示例库的副本(您需要从以下链接单击下载):
Expression Blend samples
该库包含一个名为“DataEventTrigger”的预构建触发器,您可以使用它来触发操作以响应在您的 View 模型上声明的事件。
Blend SDK 已经有了(据我所知)难题的另一部分——它已经包含一个允许您控制 Storyboard的 Action 。此操作的名称是“ControlStoryboardAction”。
您应该最终得到一些 xaml,如下所示:
<i:Interaction.Triggers>
<samples:DataEventTrigger EventName="YourEvent">
<im:ControlStoryboardAction Storyboard="{StaticResource Storyboard1}"
ControlStoryboardOption="Play"/>
</samples:DataEventTrigger>
</i:Interaction.Triggers>
将“YourEvent”替换为您在 View 模型上定义的事件的名称,并将“Storyboard1”替换为您的 Storyboard的名称。当然,名称必须完全匹配。
以下是使用的 xaml 命名空间定义:
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:im="clr-namespace:Microsoft.Expression.Interactivity.Media;assembly=Microsoft.Expression.Interactions"
xmlns:samples="clr-namespace:Expression.Samples.Interactivity;assembly=Expression.Samples.Interactivity"
原帖,编辑前:
建议您查看 Expression Blend Behaviors:
information
Blend SDK
video on behaviors
关于wpf - 当底层 Viewmodel 指示它应该时,如何在 View 触发器上设置 WPF EventTrigger?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1424202/