WPF 交互在样式中触发以调用 View 模型上的命令

标签 wpf xaml icommand

这个问题在这里已经有了答案:




8年前关闭。




Possible Duplicate:
How to add a Blend Behavior in a Style Setter



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

作为引用,请查看 MVVM Light 工具包的 EventToCommand 示例。

在这种特殊情况下,我使用 Infragistics 的 Timeline 控件,它将事件表示为 EventTitle,当单击 EventTitle 时,我想发出命令(请注意,Timeline 控件没有定义任何事件,如 EventTitleClicked)。目前我能够通过使用事件并从后面的代码调用我的 ViewModel 方法来实现该功能,而不是我想直接从 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 交互在样式中触发以调用 View 模型上的命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5759665/

相关文章:

wpf - 如何根据右键单击哪个 WPF DataGrid 行添加 ContextMenu?

c# - DataGrid 列标题中的 WPF 右对齐按钮

带有 DataTemplate 的 WPF TabControl 行为很奇怪

c# - WPF:如果 TextBox 中的文本发生更改,则更新按钮

wpf - 如何在MVVM中显示消息框

c# - 如何将关闭按钮添加到 MahApps TabControl 的选项卡?

c# - 手势 CTRL+C 和 CTRL+V 的键绑定(bind)不起作用?

c# - 在MVVM中处理事件时如何发送事件处理程序的第二个参数?

mvvm-light - 绑定(bind) ICommand 输入按下