WPF交互触发器在样式中调用视图模型上的命令

5

可能是重复问题:
如何在样式设置器中添加混合行为(Blend Behavior)

当我在样式中使用交互触发器时,出现以下错误:“triggers is not attachable element of type style”。请问这个错误实际上意味着什么以及如何解决它。

参考MVVM Light toolkit的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" />
    ....
2个回答

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

不要试图在控制之外,而是在你的掌控范围内尝试,那样才会奏效 :) - sobby01
请看我上面的帖子,我已经取得了一个文本框,并在失去焦点事件上绑定了一个命令。在视图模型上创建一个命令。 - sobby01
鼠标双击事件将被触发,您的命令将被执行。 - sobby01

-2
<TextBox x:Name="EditableControlTextBox" Loaded="RoomTextBox_Loaded">
         <interactivity:Interaction.Triggers>
              <interactivity:EventTrigger EventName="LostFocus">
                     <!--<cmd:EventToCommand Command="{Binding MyCommand}" />-->
              </interactivity:EventTrigger>
          </interactivity:Interaction.Triggers>
</TextBox>

7
不在话下,询问者想要以一种风格来完成它,在离散控制方面做到这一点是微不足道的。 - H.B.

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接