当底层 Viewmodel 指示它应该时,如何在 View 触发器上设置 WPF EventTrigger? [英] How can I Have a WPF EventTrigger on a View trigger when the underlying Viewmodel dictates it should?

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

问题描述

场景如下:

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

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>

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

in the codebehind for UnitView, I have:

public event EventHandler ActivateGlow;

在 MVVM 中很正常,我有以下用于 UnitViewModel 的 DataTemplate:

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>

最后的问题是,我如何设置一些东西以便视图模型可以触发 OnActivateGlow 事件?

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

推荐答案

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

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

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

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

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

blend SDK 已经有了(据我所知)难题的另一部分——它已经包含一个允许您控制故事板的动作.此操作的名称是ControlStoryboardAction".

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

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

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>

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

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.

以下是使用的 xaml 命名空间定义:

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"

原帖,编辑前:

建议您研究表达式混合行为:

Suggest you look into Expression Blend Behaviors:

信息

混合 SDK

行为视频

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

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