在样式的 EventTrigger 内触发命令?
如您所知,您无法将事件直接绑定到没有行为的命令:
<DataGrid>
<i:Interaction.Triggers>
<i:EventTrigger EventName="PreviewMouseDoubleClick">
<i:InvokeCommandAction Command="{Binding TradeEntryCommand"} />
</i:EventTrigger>
</i:Interaction.Triggers>
</DataGrid>
这工作得很好,但是现在我必须将其从双击 DataGrid 本身重构为双击单元格。 (我不关心单击了哪个单元格)
我希望现在在单元格样式中定义此行为,如下所示:
<Style x:Key="DefaultCellStyleBase" TargetType="{x:Type DataGridCell}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGridCell}">
<ControlTemplate.Triggers>
<EventTrigger RoutedEvent="PreviewMouseDoubleClick">
?????????
</EventTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
<!-- ... -->
</Style>
但是我如何从上面引入行为来触发命令?
高度赞赏,
As you know you can't bind an Event directly to a command without a behaviour:
<DataGrid>
<i:Interaction.Triggers>
<i:EventTrigger EventName="PreviewMouseDoubleClick">
<i:InvokeCommandAction Command="{Binding TradeEntryCommand"} />
</i:EventTrigger>
</i:Interaction.Triggers>
</DataGrid>
This works perfectly fine, however now I have to refactor this from double clicking the DataGrid itself to double clicking the Cell. (I don't care which cell was clicked)
I was hoping to define this behviour now inside the Cell Style like this:
<Style x:Key="DefaultCellStyleBase" TargetType="{x:Type DataGridCell}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGridCell}">
<ControlTemplate.Triggers>
<EventTrigger RoutedEvent="PreviewMouseDoubleClick">
?????????
</EventTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
<!-- ... -->
</Style>
But how would I bring in the behaviour from above to fire the command?
Highly appreciated,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于您正在重新模板化 DataGridCell,因此您可以将触发器添加到控件模板中的根元素。像这样的东西:
Since you are retemplating the DataGridCell, you could add the triggers to the root element in the control template. Something like:
这是我在类似情况下用于按钮命令的版本(DataGridRow 中的按钮,DataGrid 上的命令应由按钮调用,并且我需要命令中行的 DataContext)。您必须改用 doubleClick-trigger 的 InvokeCommandAction 命令,但我想它也应该可以工作。
祝你好运!
Thats a version I am using for a Button-command in a similar situation (Button in DataGridRow, Command on DataGrid should be invoked by the Button and I need the DataContext of the row in my command). You would have to use the command of the InvokeCommandAction of the doubleClick-trigger instead, but then it should work as well, I suppose.
Good luck!