如何在 mvvm 中绑定鼠标双击命令

本文关键字:鼠标 双击 命令 绑定 mvvm | 更新日期: 2023-09-27 17:49:22

我有列表视图,我想在有人双击任何位置时显示新窗口。但是我有 mvvm 应用程序,我不希望在 xaml 文件的代码隐藏中有任何功能,如下所示:如何绑定命令以双击 DataGrid 中的行以及许多其他类似的示例。我想在视图模型文件中有方法并像这样绑定它:

<ListView ... MouseDoubleClick="{Binding myfunction}"> 

谢谢

如何在 mvvm 中绑定鼠标双击命令

这是一个基于列表中单击的项目触发命令(在 ViewModel 中(的方法的工作示例。视图模型中的命令将获取"单击"项作为其参数。

我正在使用Textblock.InputBindings,这可能是Blachshma链接的Blend SDK的一部分,但你不需要任何其他DLL就可以工作。

在我的示例中,ViewModel 绑定到 UserControl 的 DataContext,这就是为什么我需要使用 RelativeSource FindAncestor 从我的 TextBlock 中查找 ViewModel。

编辑:通过将文本的宽度绑定到列表框的实际宽度来修复宽度问题。

只有一个问题,双击只有在您在文本块中的文本内单击时才有效,即使列表本身要宽得多。
    <ListView ItemsSource="{Binding Model.TablesView}"   Grid.Row="1" 
              SelectedItem="{Binding Model.SelectedTable, Mode=TwoWay}"  >
        <ListView.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Path=.}" 
                   Width="{Binding Path=ActualWidth, 
                             RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListView}}}" >
                    <TextBlock.InputBindings>
                        <MouseBinding MouseAction="LeftDoubleClick" Command="{Binding DataContext.MoveItemRightCommand,
                                        RelativeSource={RelativeSource FindAncestor, 
                                        AncestorType={x:Type UserControl}}}"
                                      CommandParameter="{Binding .}"/>
                    </TextBlock.InputBindings>
                </TextBlock>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

可以使用附加属性绑定所需的任何事件。

对于MouseDoubleClick

namespace Behavior
{
public class MouseDoubleClick
{
    public static DependencyProperty CommandProperty =
        DependencyProperty.RegisterAttached("Command",
        typeof(ICommand),
        typeof(MouseDoubleClick),
        new UIPropertyMetadata(CommandChanged));
    public static DependencyProperty CommandParameterProperty =
        DependencyProperty.RegisterAttached("CommandParameter",
                                            typeof(object),
                                            typeof(MouseDoubleClick),
                                            new UIPropertyMetadata(null));
    public static void SetCommand(DependencyObject target, ICommand value)
    {
        target.SetValue(CommandProperty, value);
    }
    public static void SetCommandParameter(DependencyObject target, object value)
    {
        target.SetValue(CommandParameterProperty, value);
    }
    public static object GetCommandParameter(DependencyObject target)
    {
        return target.GetValue(CommandParameterProperty);
    }
    private static void CommandChanged(DependencyObject target, DependencyPropertyChangedEventArgs e)
    {
        Control control = target as Control;
        if (control != null)
        {
            if ((e.NewValue != null) && (e.OldValue == null))
            {
                control.MouseDoubleClick += OnMouseDoubleClick;
            }
            else if ((e.NewValue == null) && (e.OldValue != null))
            {
                control.MouseDoubleClick -= OnMouseDoubleClick;
            }
        }
    }
    private static void OnMouseDoubleClick(object sender, RoutedEventArgs e)
    {
        Control control = sender as Control;
        ICommand command = (ICommand)control.GetValue(CommandProperty);
        object commandParameter = control.GetValue(CommandParameterProperty);
        command.Execute(commandParameter);
    }
}
}

在 Xaml 中:

 <ListBox Behavior:MouseDoubleClick.Command="{Binding ....}"
          Behavior:MouseDoubleClick.CommandParameter="{Binding ....}"/>

最简单的方法是使用System.Windows.InteractivityMicrosoft.Expression.Interactions(两者都可以通过Blend SDK免费获得(

因此,首先将以下命名空间添加到视图中

xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"

接下来,捕获 DoubleClick 事件并将其传递给命令:

<ListView ..... >
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="MouseDoubleClick">
            <local:EventToCommand Command="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=DataContext.myfunction}" />
         </i:EventTrigger
    </i:Interaction.Triggers>
</ListView>

注意:使用的EventToCommand是 MVVM Light Toolkit 中的,可在此处下载。它的作用是在触发事件后立即执行命令(myFunction(。

这是基于以下假设:myFunction命令位于 ListView 用户的数据上下文中。否则,请将 EventToCommand 的绑定修改为命令所在的任何位置。