WPF 交互在样式中触发以调用视图模型上的命令 [英] WPF Interaction triggers in a Style to invoke commands on View Model

查看:25
本文介绍了WPF 交互在样式中触发以调用视图模型上的命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能的重复:
如何在样式设置器中添加混合行为

当我在样式中使用交互触发器时,出现以下错误,触发器不是样式类型的可附加元素".任何解释此错误的实际含义以及如何解决它.

when I use an interaction trigger in a style, I am getting the following error, 'triggers is not attachable element of type style'. Any explanation what this error actually means and how to solve it.

作为参考,请查看 MVVM Light 工具包的 EventToCommand 示例.

For reference take a look at MVVM Light toolkit's EventToCommand example.

在这种特殊情况下,我使用来自 Infragistics 的 Timeline 控件,它将事件表示为 EventTitle,当单击 EventTitle 时,我想发出命令(请注意,Timeline 控件没有定义任何事件,如 EventTitleClicked).目前我能够通过使用事件并从后面的代码调用我的 ViewModel 方法来实现该功能,而不是我想直接从 xaml 调用命令.

In this particular case, I am using Timeline control from Infragistics which represents events as EventTitle and when the EventTitle is clicked, I would like to raise the command (Note that Timeline control doesn't define any event like EventTitleClicked). Currently I am able to achieve the functionality by using events and calling my ViewModel method from the code behind, instead I would like to invoke the command directly from xaml.

<Style x:Key="EventTitleTopStyle" TargetType="igTl:EventTitle">
    <!-- The following is not working -->
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="MouseLeftButtonDown">
            <!--<cmd:EventToCommand Command="{Binding MyCommand}" />-->
        </i:EventTrigger>
    </i:Interaction.Triggers>

   <!-- Using event setter instead to achieve the same -->
    <EventSetter Event="MouseLeftButtonDown" Handler="TopTitleMouseLeftButtonDown" />
    ....

推荐答案

<interactivity:Interaction.Triggers>
     <interactivity:EventTrigger EventName="MouseDoubleClick">
          <behaviours:ExecuteCommandAction Command="{Binding Path=DataContext.YourCommand, RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type UserControl}}}" 
               CommandParameter="{Binding }"/>
     </interactivity:EventTrigger>
</interactivity:Interaction.Triggers>

这篇关于WPF 交互在样式中触发以调用视图模型上的命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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