如何在数据网格中执行LeftDoubleClick

本文关键字:执行 LeftDoubleClick 网格 数据网 数据 | 更新日期: 2023-09-27 18:05:04

我实现了:

    <DataGrid Name="DataGridProcesso" AutoGenerateColumns="False" ItemsSource="{Binding  CollectionView}"  CanUserReorderColumns="False" CanUserSortColumns="True" AlternatingRowBackground="#D1DFE7" IsSynchronizedWithCurrentItem="True">
          <DataGrid.InputBindings>
              <MouseBinding MouseAction="LeftDoubleClick" 
              Command="{Binding ItemDoubleClickCommand}" />
          </DataGrid.InputBindings>
    ...

但双击只工作,如果我点击六次(3次双击)我认为第一次点击发生在内部单元格,单元格后,最后在行(我只认为,我不确定),我只是想当LeftDoubleClick发生在单元格触发我的事件。

怎么了?

我的DataGrid的完整代码:

<DataGrid Name="DataGridProcesso" AutoGenerateColumns="False" ItemsSource="{Binding  CollectionView}"  CanUserReorderColumns="False" CanUserSortColumns="True" AlternatingRowBackground="#D1DFE7" IsSynchronizedWithCurrentItem="True">
            <DataGrid.InputBindings>
                <MouseBinding MouseAction="LeftDoubleClick" Command="{Binding ItemDoubleClickCommand}" />
            </DataGrid.InputBindings>
            <DataGrid.Resources>
                <Style TargetType="{x:Type DataGridRow}">
                    <Setter Property="Padding" Value="0,10,0,10" />
                    <Setter Property="BorderBrush" Value="Transparent"/>
                    <Setter Property="BorderThickness" Value="0,0,0,0"/>
                </Style>
                <Style TargetType="{x:Type DataGridCell}">
                    <Setter Property="BorderBrush" Value="#CCC"/>
                    <Setter Property="BorderThickness" Value="0,0,1,0"/>
                    <Setter Property="Cursor" Value="Hand" />
                    <Setter Property="FontFamily" Value="Segoe" />
                    <Setter Property="FontSize" Value="14" />
                </Style>
            </DataGrid.Resources>
            <DataGrid.Columns>
                <DataGridTemplateColumn Header="Data" Width="Auto" HeaderStyle="{StaticResource DataGridColumnHeaderStyle}">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Label Content="{Binding Path=Data,Mode=OneTime}" ToolTip="Data" HorizontalAlignment="Right"></Label>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTemplateColumn Header="Número"  Width="Auto" HeaderStyle="{StaticResource DataGridColumnHeaderStyle}">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Label Content="{Binding Path=Numero,Mode=OneTime}" ToolTip="Número"></Label>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTemplateColumn Header="Descrição"  Width="*" HeaderStyle="{StaticResource DataGridColumnHeaderStyle}">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Label Content="{Binding Path=Descricao, Mode=OneTime}" ToolTip="Descrição"/>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTemplateColumn Header="Andamento"  Width="Auto" HeaderStyle="{StaticResource DataGridColumnHeaderStyle}">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Label Content="{Binding Path=Andamento.Descricao, Mode=OneTime}" ToolTip="Andamento"/>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTemplateColumn Header="Lembrete"  Width="Auto" HeaderStyle="{StaticResource DataGridColumnHeaderStyle}">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Label Content="{Binding Path=Lembrete, Mode=OneTime}" ToolTip="Lembrete"/>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>

ViewModel的一部分:

private ObservableCollection<Processo> _processos;
    public ObservableCollection<Processo> Processos
    {
        get { return _processos; }
        set
        {
            if (value != _processos)
            {
                _processos = value;
                NotifyPropertyChanged(nameof(Processos));
            }
        }
    }
    private ICollectionView _collectionView;
    public ICollectionView CollectionView
    {
        get
        {
            return _collectionView;
        }
        set
        {
            _collectionView = value;
            NotifyPropertyChanged(nameof(CollectionView));
        }
    }
    public ICommand ItemDoubleClickCommand => new Command<MainWindowViewModel>(a =>
    {
        AlterarProcesso((Processo)CollectionView.CurrentItem);
    });

如何在数据网格中执行LeftDoubleClick

尝试将IsSynchronizedWithCurrentItem="True"添加到DataGrid属性中。
再加入CommandParameter="{Binding CollectionView/}"

 <DataGrid Name="DataGridProcesso" IsSynchronizedWithCurrentItem="True" AutoGenerateColumns="False" ItemsSource="{Binding  CollectionView}"  CanUserReorderColumns="False" CanUserSortColumns="True" AlternatingRowBackground="#D1DFE7" IsSynchronizedWithCurrentItem="True">
      <DataGrid.InputBindings>
          <MouseBinding MouseAction="LeftDoubleClick" 
          Command="{Binding ItemDoubleClickCommand}"
          CommandParameter="{Binding CollectionView/}"/>
      </DataGrid.InputBindings>

注意CommandParameter中的/,这确保它绑定到当前项。然后,当它与IsSynchronizedWithCurrentItem="True"一起使用时,它确保它绑定到选定的项目。