这个问题在这里已经有了答案:
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/