WPF - 将 EventSetter 连接到 ICommand

发布于 2025-01-11 06:04:09 字数 892 浏览 0 评论 0原文

我正在寻找一种解决方案,双击 DataGridRow,使用 ICommand 调用 ViewModel 中的方法。

我的 DataGrid 的 DataGridRow 样式有以下代码:

<DataGrid.Resources>
    <Style TargetType="{x:Type DataGridRow}">
        <EventSetter Event="MouseDoubleClick"
                     Handler="DataGridRow_MouseDoubleClick" />
    </Style>
</DataGrid.Resources>

这有效,但是...
我需要在 XAML 的代码隐藏中使用 DataGridRow_MouseDoubleClick 方法。然后在该方法中我需要调用 ViewModel 中的方法。
我想绕过代码隐藏并直接使用 ICommand 调用 ViewModel 中的方法。

我发现这段代码很优雅,但是只要我(向左)双击 DataGrid,就会调用该方法。

<DataGrid>
    <DataGrid.InputBindings>
        <MouseBinding Gesture="LeftDoubleClick"
                      Command="{Binding MyCallback}" />
    </DataGrid.InputBindings>-->
</DataGrid>

我只能允许双击 DataGridRow。

有什么建议吗?

/BR
斯特夫

I'm looking for a solution where I double click on a DataGridRow that calls a method in my ViewModel with an ICommand.

I have this code for my DataGrid's DataGridRow style:

<DataGrid.Resources>
    <Style TargetType="{x:Type DataGridRow}">
        <EventSetter Event="MouseDoubleClick"
                     Handler="DataGridRow_MouseDoubleClick" />
    </Style>
</DataGrid.Resources>

This works, but...
I need to have the method DataGridRow_MouseDoubleClick in the XAML's code-behind. Then in that method I need to call the method in my ViewModel.
I would like to bypass the code-behind and directly call the method in the ViewModel with an ICommand.

I found this code which was elegant but, calls the method whereever I (left) double click on the DataGrid.

<DataGrid>
    <DataGrid.InputBindings>
        <MouseBinding Gesture="LeftDoubleClick"
                      Command="{Binding MyCallback}" />
    </DataGrid.InputBindings>-->
</DataGrid>

I can only allow double click on a DataGridRow.

Any suggestions?

/BR
Steffe

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

白鸥掠海 2025-01-18 06:04:09

您可以将事件处理程序替换为执行命令的附加行为:

public static class DataGridRowExtensions
{
    public static readonly DependencyProperty MouseDoubleClickCommandProperty =
        DependencyProperty.RegisterAttached(
            "MouseDoubleClickCommand",
            typeof(ICommand),
            typeof(DataGridRowExtensions),
            new FrameworkPropertyMetadata(default(ICommand), new PropertyChangedCallback(OnSet))
    );

    public static ICommand GetMouseDoubleClickCommand(DataGridRow target) =>
        (ICommand)target.GetValue(MouseDoubleClickCommandProperty);

    public static void SetMouseDoubleClickCommand(DataGridRow target, ICommand value) =>
        target.SetValue(MouseDoubleClickCommandProperty, value);

    private static void OnSet(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        DataGridRow row = (DataGridRow)d;
        row.MouseDoubleClick += Row_MouseDoubleClick;
    }

    private static void Row_MouseDoubleClick(object sender, MouseButtonEventArgs e)
    {
        DataGridRow row = (DataGridRow)sender;
        ICommand command = GetMouseDoubleClickCommand(row);
        if (command != null)
            command.Execute(default);
    }
}

XAML:

<Style TargetType="{x:Type DataGridRow}">
    <Setter Property="local:DataGridRowExtensions.MouseDoubleClickCommand"
            Value="{Binding DataContext.MyCallback,
                RelativeSource={RelativeSource AncestorType=DataGrid}}" />
</Style>

You could replace the event handler with an attached behaviour that executes the command:

public static class DataGridRowExtensions
{
    public static readonly DependencyProperty MouseDoubleClickCommandProperty =
        DependencyProperty.RegisterAttached(
            "MouseDoubleClickCommand",
            typeof(ICommand),
            typeof(DataGridRowExtensions),
            new FrameworkPropertyMetadata(default(ICommand), new PropertyChangedCallback(OnSet))
    );

    public static ICommand GetMouseDoubleClickCommand(DataGridRow target) =>
        (ICommand)target.GetValue(MouseDoubleClickCommandProperty);

    public static void SetMouseDoubleClickCommand(DataGridRow target, ICommand value) =>
        target.SetValue(MouseDoubleClickCommandProperty, value);

    private static void OnSet(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        DataGridRow row = (DataGridRow)d;
        row.MouseDoubleClick += Row_MouseDoubleClick;
    }

    private static void Row_MouseDoubleClick(object sender, MouseButtonEventArgs e)
    {
        DataGridRow row = (DataGridRow)sender;
        ICommand command = GetMouseDoubleClickCommand(row);
        if (command != null)
            command.Execute(default);
    }
}

XAML:

<Style TargetType="{x:Type DataGridRow}">
    <Setter Property="local:DataGridRowExtensions.MouseDoubleClickCommand"
            Value="{Binding DataContext.MyCallback,
                RelativeSource={RelativeSource AncestorType=DataGrid}}" />
</Style>
漫漫岁月 2025-01-18 06:04:09

首先,在您的项目中安装下面提到的 Nuget 包。

Microsoft.Xaml.Behaviors.Wpf

然后将以下引用添加到相关的 xaml 字段。

xmlns:i="http://schemas.microsoft.com/xaml/behaviors"

下一步,您可以按如下方式应用双击功能。

<i:Interaction.Triggers>
     <i:EventTrigger EventName="MouseDoubleClick">
          <i:InvokeCommandAction Command="{Binding MyCallback}"/>
     </i:EventTrigger>
</i:Interaction.Triggers>

First, install the Nuget package mentioned below in your project.

Microsoft.Xaml.Behaviors.Wpf

Then add the following reference to the relevant xaml field.

xmlns:i="http://schemas.microsoft.com/xaml/behaviors"

As the next step, you can apply the double click function as follows.

<i:Interaction.Triggers>
     <i:EventTrigger EventName="MouseDoubleClick">
          <i:InvokeCommandAction Command="{Binding MyCallback}"/>
     </i:EventTrigger>
</i:Interaction.Triggers>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文