【问题标题】:How do i handle cell double click event on WPF DataGrid, equivalent to windows DataGrid's Events?如何处理 WPF DataGrid 上的单元格双击事件,相当于 windows DataGrid 的事件?
【发布时间】:2013-01-11 12:45:36
【问题描述】:

如您所知,在 windows C# 的 gridview 中,如果我们想处理单元格上的单击/双击事件,则存在诸如 CellClick、CellDoubleClick 等事件。

所以,我想做与带有 WPF DataGrid 的 windows gridview 一样的操作。到目前为止我已经搜索过,但答案既不适用也不有用。他们中的一些人说使用 MouseDoubleClick 事件,但在这种情况下,我们必须检查每一行以及该行中的项目,因此检查每个单元格的数据非常耗时,而且时间在这里是最重要的。

我的 DataGrid 绑定到 DataTable 并且 AutoGeneratedColumn 为 False。如果您的答案基于 AutoGeneratedColumn=True ,那么这是不可能的。甚至,我正在根据数据更改数据网格单元格的样式,因此无法更改 AutoGeneratedColumn 属性。

单元格单击/双击事件应该与 windows 网格的事件一样快。如果可以,请告诉我怎么做,如果不行,那还有什么替代方法?

请帮帮我.....

非常感谢....

【问题讨论】:

    标签: wpf events datagrid cell


    【解决方案1】:

    我知道这对聚会来说可能有点晚了,但这可能对以后的其他人有用。

    在您的 MyView.xaml 中:

    <DataGrid x:Name="MyDataGrid" ...>
        <DataGrid.Resources>
            <Style TargetType="{x:Type DataGridCell}">
                <EventSetter Event="MouseDoubleClick" Handler="DataGridCell_MouseDoubleClick"/>
            </Style>
        </DataGrid.Resources>
    
        <!-- TODO: The rest of your DataGrid -->
    </DataGrid>
    

    在您的 MyView.xaml.cs 中:

    private void DataGridCell_MouseDoubleClick(object sender, MouseButtonEventArgs e)
    {
        var dataGridCellTarget = (DataGridCell)sender;
        // TODO: Your logic here
    }
    

    【讨论】:

    • 优秀的答案
    【解决方案2】:

    另一种方法是定义DataGridTemplateColumn,而不是使用DataGridCheckBoxColumn、DataGridComboBoxColumn 等预定义列,然后将事件处理程序添加到数据模板中定义的UI 元素。

    下面我为TextBlock Cell 定义了一个MouseDown 事件处理程序。

    <DataGrid AutoGenerateColumns="False">
        <DataGrid.Columns>
    
            <DataGridTemplateColumn>
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock MouseDown="TextBlock_MouseDown"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>
    </DataGrid>
    

    在代码隐藏文件中:

    private void TextBlock_MouseDown(object sender, MouseButtonEventArgs e)
    {
        TextBlock block = sender as TextBlock;
        if (block != null)
        {
            // Some Logic
            // block.Text
        }
    }
    

    【讨论】:

    • 谢谢,但是在哪里编写这段代码,因为我在 cs 文件文件中没有代码,除了与数据库的连接,我正在根据该位置的数据更改特定网格的样式,所以在哪里定义这个我不知道的代码......即使我现在正在尝试做......感谢回复......
    • 您必须在 DataGrid.Columns 部分添加代码。我已经更新了我的答案....
    • 如何在代码后面或 xaml 中生成 DataGrid 列?
    【解决方案3】:

    我知道编写 WPF 有时是一个 PITA。无论如何,您必须在这里处理MouseDoubleClick 事件。然后搜索源对象层次结构以找到DataGridRow 并对其进行任何处理。

    更新:示例代码

    XAML

    <dg:DataGrid MouseDoubleClick="OnDoubleClick" />
    

    后面的代码

    private void OnDoubleClick(object sender, MouseButtonEventArgs e)
    {
        DependencyObject source = (DependencyObject) e.OriginalSource;
        var row = GetDataGridRowObject(source);
        if (row == null)
        {
             return;
        }
        else
        {
            // Do whatever with it
        }
        e.Handled = true;
    }
    
    private DataGridRow GetDataGridRowObject(DependencyObject source)                               
    {
        // Write your own code to recursively traverse up via the source
        // until you find a DataGridRow object. Otherwise return null.
    }
    

    }

    【讨论】:

      【解决方案4】:

      我用过这样的东西:

      <DataGrid.InputBindings>
          <MouseBinding Gesture="LeftDoubleClick" Command="{Binding ShowOverlay}" CommandParameter="{Binding Parameter}" />
      </DataGrid.InputBindings>
      

      并在我的视图模型中处理我的命令。

      【讨论】:

        猜你喜欢
        • 2017-01-15
        • 2019-02-09
        • 2013-08-31
        • 2011-10-12
        • 1970-01-01
        • 2016-03-29
        • 1970-01-01
        • 2011-08-22
        • 2017-04-06
        相关资源
        最近更新 更多