当底层Viewmodel指示它应该在View触发器上有一个WPF EventTrigger时该怎么办?

Fir*_*oso 25 wpf events eventtrigger mvvm

这是场景:

我有以下用户控件,想法是它的视图模型应该能够向视图发出信号,它需要"激活发光",从而播放故事板.

<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>
Run Code Online (Sandbox Code Playgroud)

在UnitView的代码隐藏中,我有:

public event EventHandler ActivateGlow;
Run Code Online (Sandbox Code Playgroud)

并且在MVVM中非常正常,我为UnitViewModel提供了以下DataTemplate:

<DataTemplate DataType="{x:Type vm:UnitViewModel}">
    <vw:UnitView d:DesignWidth="150" d:DesignHeight="100" />
</DataTemplate>
Run Code Online (Sandbox Code Playgroud)

最后一个问题是,我如何设置一些东西,以便viewmodel可以触发OnActivateGlow事件?

Phi*_*hil 12

更新:Firoso,如您在评论中所提到的(我认为 - 即未经测试)能够使用混合行为组件来满足您的要求.

除了下载和安装SDK之外.获取表达式混合样本库的副本(您需要从以下链接单击"下载"): Expression Blend样本

该库包含一个名为"DataEventTrigger"的预构建触发器,您可以使用该触发器触发操作以响应在viewmodel上声明的事件.

混合SDK已经(我可以告诉)另一个难题 - 它已经包含一个允许你控制故事板的动作.此操作的名称是"ControlStoryboardAction".

你应该得到一些看起来像这样的xaml:

    <i:Interaction.Triggers>
        <samples:DataEventTrigger EventName="YourEvent">
            <im:ControlStoryboardAction Storyboard="{StaticResource Storyboard1}" 
                   ControlStoryboardOption="Play"/>
        </samples:DataEventTrigger>
    </i:Interaction.Triggers>
Run Code Online (Sandbox Code Playgroud)

将"YourEvent"替换为您在viewmodel上定义的事件的名称,并将"Storyboard1"替换为故事板的名称.当然,名称必须完全匹配.

以下是使用的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"
Run Code Online (Sandbox Code Playgroud)

原始帖子,编辑前:

建议你研究Expression Blend Behaviors:

信息

混合SDK

关于行为的视频


ken*_*ner 6

您还可以在viewmodel上放置一个布尔值IsGlowing属性,并在您的样式中使用数据触发器

<Rectangle.Style>  
    <Style TargetType="{x:Type Rectangle}">  
        <Style.Triggers>  
            <DataTrigger Binding="{Binding Path=IsGlowing}" Value="True">  
                <DataTrigger.EnterActions>  
                    <BeginStoryboard>  
                        <Storyboard>  
                            ...  
                        </Storyboard>  
                    </BeginStoryboard>  
                </DataTrigger.EnterActions>  
            </DataTrigger>  
        </Style.Triggers>  
    </Style>  
</Rectangle.Style>  
Run Code Online (Sandbox Code Playgroud)