当底层Viewmodel决定应该在View触发器上时,如何使用WPF EventTrigger? [英] How can I Have a WPF EventTrigger on a View trigger when the underlying Viewmodel dictates it should?

查看:175
本文介绍了当底层Viewmodel决定应该在View触发器上时,如何使用WPF EventTrigger?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是这种情况:



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

 < UserControl x:Class =View.UnitView...> 
...
< Storyboard x:Key =ActivateGlow>
...
< / Storyboard>
...
<! - 无效绑定!不依赖对象 - >
< EventTrigger RoutedEvent ={Binding OnActivateGlow}>
< BeginStoryboard Storyboard ={StaticResource ActivateGlow}/>
< / EventTrigger>
< / UserControl>

在UnitView的codebehind中,我有:



公开事件EventHandler ActivateGlow;

  

在MVVM中非常正常,我对UnitViewModel有以下DataTemplate:

 < DataTemplate DataType ={x:Type vm:UnitViewModel}> 
< vw:UnitView d:DesignWidth =150d:DesignHeight =100/>
< / DataTemplate>

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

解决方案

更新:Firoso,您应该能够(我认为 - 即未经测试)能够使用混合行为组件来满足您的需求。



除了下载和安装SDK。获取表达式混合样本库的副本(您需要从以下链接中单击下载):
表达式混合示例



此库包含一个名为DataEventTrigger的预构建触发器,您可以使用它来触发操作以响应viewmodel上声明的事件。 / p>

混合SDK已经(从我可以告诉)另一个拼图 - 它已经包括一个允许您控制故事板的动作。此操作的名称为ControlStoryboardAction。



您应该最终得到一些如下所示的xaml:

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

将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

编辑前的原始帖子



建议您查看Expression Blend行为:



信息



Blend SDK



视频上的行为


Here's the scenario:

I have the following user control, the idea is that it's view model should be able to signal to the view that it needs to "Activate the Glow", thereby playing the 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>

in the codebehind for UnitView, I have:

public event EventHandler ActivateGlow;

and as is pretty normal in MVVM, I have the following DataTemplate for UnitViewModel:

<DataTemplate DataType="{x:Type vm:UnitViewModel}">
    <vw:UnitView d:DesignWidth="150" d:DesignHeight="100" />
</DataTemplate>

The ulitmate question is, how can I set up something so that the viewmodel can fire the OnActivateGlow event?

解决方案

Update: Firoso, as mentioned in the comments you should be able to (I think - i.e. untested) be able to use the blend behavior components to cover your requirement.

In addition to downloading and installing the SDK. Get a copy of the expression blend samples library (you'll need to click on Downloads from the following link): Expression Blend samples

This library contains a prebuilt trigger called 'DataEventTrigger' which you can use to trigger actions in response to an event declared on your viewmodel.

The blend SDK already has (from what I can tell) the other piece of the puzzle - it already includes an action which allows you to control storyboards. The name of this action is 'ControlStoryboardAction'.

You should end up with some xaml which looks like this:

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

Replace 'YourEvent' with the name of the event you have defined on your viewmodel, and replace 'Storyboard1' with the name of your storyboard. Of course the names will have to match exactly.

Here are the xaml namespace definitions used:

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"

Original post, before edit:

Suggest you look into Expression Blend Behaviors:

information

Blend SDK

video on behaviors

这篇关于当底层Viewmodel决定应该在View触发器上时,如何使用WPF EventTrigger?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆