在样式的 EventTrigger 内触发命令?

发布于 2024-11-06 05:57:47 字数 1103 浏览 0 评论 0原文

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

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

扎心 2024-11-13 05:57:47

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

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

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>
晚雾 2024-11-13 05:57:47

这是我在类似情况下用于按钮命令的版本(DataGridRow 中的按钮,DataGrid 上的命令应由按钮调用,并且我需要命令中行的 DataContext)。您必须改用 doubleClick-trigger 的 InvokeCommandAction 命令,但我想它也应该可以工作。

祝你好运!

    <DataTemplate>
            <TextBlock>                             
           <Button x:Name="cmdButton"                            
                                    Command="{Binding Path=DataContext.CommandNameInViewModel, 
                                        RelativeSource={RelativeSource AncestorType={x:Type TypeOfAncestorWithTheViewModel}}}"
                                    CommandParameter="{Binding}" >      
                                    Do something
        </Button>

    </TextBlock>  
</DataTemplate>     

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!

    <DataTemplate>
            <TextBlock>                             
           <Button x:Name="cmdButton"                            
                                    Command="{Binding Path=DataContext.CommandNameInViewModel, 
                                        RelativeSource={RelativeSource AncestorType={x:Type TypeOfAncestorWithTheViewModel}}}"
                                    CommandParameter="{Binding}" >      
                                    Do something
        </Button>

    </TextBlock>  
</DataTemplate>     
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文