WPF Interaction在Style中触发,以在View Model上调用命令

时间:2011-04-22 20:17:30

标签: wpf xaml icommand

  

可能重复:
  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" />
    ....

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:-3)

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