代码之家  ›  专栏  ›  技术社区  ›  myermian

wpf datagrid:commandbinding到双击而不是使用事件

  •  26
  • myermian  · 技术社区  · 15 年前

    我知道如何将mousedoubleclick事件与datagrid一起使用来获取selectedValue,但是如何使用命令绑定呢?这样我的视图模型就可以处理逻辑。

    到目前为止,我有以下几点:

    <DataGrid Name="TestGrid" Grid.Row="2" Grid.ColumnSpan="2" AutoGenerateColumns="True" MouseDoubleClick="TestGrid_MouseDoubleClick"
              ItemsSource="{Binding Registrations}" SelectedValue="{Binding CurrentRegistration}" IsReadOnly="True" AlternationCount="2" GridLinesVisibility="None">
    

    我想去掉鼠标双击并适当地替换它。

    4 回复  |  直到 12 年前
        1
  •  63
  •   Mizipzor    12 年前

    这里不需要附加行为或自定义DataGrid子类。

    在你的 DataGrid 绑定 ItemsSource ICollectionView . 这里的诀窍是设置 IsSynchronizedWithCurrentItem="True" 这意味着选定的行将是当前项。

    技巧的第二部分是绑定 CommandParameter 以正斜杠语法指向当前项。

    当双击一行时,命令将以单击的行作为参数执行。

    <DataGrid
        ItemsSource="{Binding CollectionView}"
        IsSynchronizedWithCurrentItem="True">
        <DataGrid.InputBindings>
            <MouseBinding
                MouseAction="LeftDoubleClick"
                Command="{Binding DoubleClickCommand}"
                CommandParameter="{Binding CollectionView/}"/>
        </DataGrid.InputBindings>
    </DataGrid>
    

    这就是视图模型(简化)版本的外观:

    class MyViewModel
    {
        public ICollectionView CollectionView { get; set; }
    
        public ICommand DoubleClickCommand { get; set; }
    }
    
        2
  •  17
  •   Tamar Cohen    12 年前

    另一种解决方案是添加输入绑定,并将SelectedItem绑定到属性,以便您知道选择了哪个属性:

    <DataGrid SelectedItem="{Binding SelectedItem}">
          <DataGrid.InputBindings>
              <MouseBinding Gesture="LeftDoubleClick" Command="{Binding SomeCommand}"/>
         </DataGrid.InputBindings>
    </DataGrid>
    
        3
  •  2
  •   vortexwolf    15 年前

    使用 this library

    绑定到DataGrid事件的示例:

    <DataGrid xmlns:command="clr-namespace:AttachedCommandBehavior;assembly=AttachedCommandBehavior"
        command:CommandBehavior.Event="MouseDoubleClick"
        command:CommandBehavior.Command="{Binding TestCommand}" />
    

    但此代码更好,因为只在行单击时引发:

    <DataGrid>
        <DataGrid.Resources>
            <Style TargetType="DataGridRow">
                <Setter Property="command:CommandBehavior.Event" Value="MouseDoubleClick"/>
                <Setter Property="command:CommandBehavior.Command" Value="{Binding DataContext.TestCommand, RelativeSource={RelativeSource FindAncestor, AncestorType=DataGrid}}"/>
            </Style>
        </DataGrid.Resources>
    </DataGrid>
    
        4
  •  1
  •   Catalin Pop    13 年前

    或者,可以创建派生类

    public class CustomDataGrid : DataGrid
    {
        public ICommand DoubleClickCommand
        {
            get { return (ICommand)GetValue(DoubleClickCommandProperty); }
            set { SetValue(DoubleClickCommandProperty, value); }
        }
    
        // Using a DependencyProperty as the backing store for DoubleClickCommand.  This    enables animation, styling, binding, etc...
        public static readonly DependencyProperty DoubleClickCommandProperty =
            DependencyProperty.Register("DoubleClickCommand", typeof(ICommand), typeof(CustomDataGrid), new UIPropertyMetadata());
    
        public CustomDataGrid()
            : base()
        {            
            this.PreviewMouseDoubleClick += new MouseButtonEventHandler(CustomDataGrid_PreviewMouseDoubleClick);
        }
    
    
        void CustomDataGrid_PreviewMouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (DoubleClickCommand != null)
            {
                DoubleClickCommand.Execute(null);
            }
        }
    
    
    }
    

    在XAML中,只需绑定到新创建的命令

    <CustomDataGrid DoubleClickCommand="{Binding DoubleClickCommand}">