-->

我怎么能有一个视图触发一个WPF EventTrigger当底层视图模型决定了它应注意什么?(How

2019-07-18 00:38发布

这里的情景:

我有以下的用户控件,这个想法是,它的视图模型应该能够发出信号,告知它需要“启动发热”,从而起到故事板视图。

<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>

该ulitmate问题是,我如何设置的东西,这样的视图模型能发射OnActivateGlow事件?

Answer 1:

更新:Firoso,如在评论中提到的,你应该可以(我认为-即未经)能够使用混合行为组件来覆盖你的要求。

除了下载和安装SDK。 获得Expression Blend的样本库的副本(你需要点击下载从以下链接): Expression Blend的样本

这个库包含一个名为“DataEventTrigger”你可以用它来触发响应行动在您的视图模型声明的事件触发的预建。

该混合SDK已经(从我可以告诉)其他的一块拼图 - 它已经包含一个行动,这使您可以控制故事板。 这个动作的名称是“ControlStoryboardAction”。

您应该结束了与一些XAML看起来像这样:

    <i:Interaction.Triggers>
        <samples:DataEventTrigger EventName="YourEvent">
            <im:ControlStoryboardAction Storyboard="{StaticResource Storyboard1}" 
                   ControlStoryboardOption="Play"/>
        </samples:DataEventTrigger>
    </i:Interaction.Triggers>

替换“YourEvent”与你有你的视图模型中定义的事件的名称,并与你的故事板的名称取代“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"

原帖,编辑之前:

建议你看看Expression Blend的行为:

信息

混合SDK

视频行为



Answer 2:

你也可以把一个布尔IsGlowing财产上的视图模型,并在你的风格使用datatriggers

<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>  


Answer 3:

我相信你会绑定到RoutedEvent的实例,而不是一个CLR事件。

我还没有尝试过,但这样的事情应该工作:

public class UnitView
{
    public static readonly RoutedEvent ActivateGlowEvent
        = EventManager.RegisterRoutedEvent(
              "ActivateGlow", RoutingStrategy.Bubble,
              typeof(RoutedEventHandler), typeof(UnitView)
          );

    public void RaiseActivateGlowEvent()
    {
        RaiseEvent(new RoutedEventArgs(ActivateGlowEvent));
    }
}


Answer 4:

我发现解决方法之一是使用数据触发对包含上述控制一个DataTemplate ......大概不会然而要做到这一点的最好办法。 我还是开到更好的想法。



文章来源: How can I Have a WPF EventTrigger on a View trigger when the underlying Viewmodel dictates it should?