在样式的 EventTrigger 中触发命令? [英] Firing a Command within EventTrigger of a style?

查看:21
本文介绍了在样式的 EventTrigger 中触发命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如您所知,您不能将事件直接绑定到没有行为的命令:

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>

这工作得很好,但是现在我必须将其从双击 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)

我希望现在在 Cell Style 中定义这种行为,如下所示:

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?

高度赞赏,

推荐答案

由于您正在重新模板化 DataGridCell,因此您可以将触发器添加到控件模板中的根元素.类似的东西:

Since you are retemplating the DataGridCell, you could add the triggers to the root element in the control template. Something like:

<ControlTemplate TargetType="{x:Type DataGridCell}">
    <Grid x:Name="root" Background="Transparent">
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="PreviewMouseDoubleClick">
                <i:InvokeCommandAction Command="{Binding TradeEntryCommand}" />
            </i:EventTrigger>                            
        </i:Interaction.Triggers>
    </Grid>
</ControlTemplate>

这篇关于在样式的 EventTrigger 中触发命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆