我如何处理 WPF DataGrid 上的单元格双击事件,相当于 windows DataGrid 的事件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14557633/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 23:39:24  来源:igfitidea点击:

How do i handle cell double click event on WPF DataGrid, equivalent to windows DataGrid's Events?

wpfeventsdatagridcell

提问by iVad

As you know, in windows C#'s gridview, if we want to handle a click/double click event on cell then there are events like CellClick, CellDoubleClick, etc.

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

So, i wanna do same like as windows gridview with WPF DataGrid. I have searched so far but neither answer is applicable nor useful. Some of them says use the MouseDoubleClick event but, in this event, we have to check for each row as well as item in that row, so it is time consuming to check every cell for data and timing is most important here.

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

My DataGrid is bounded to DataTable and AutoGeneratedColumn is False. If your answer is based on AutoGeneratedColumn=True then it is not possible. Even, i 'm changing the styles of datagrid cell according to data, so there is no way to change AutoGeneratedColumn property.

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

A Cell Clicking/Double Clicking event should be as faster as windows grid's event. If it is possible then tell me how, and if not, then what is the alternative to do it?

单元格单击/双击事件应该与 Windows 网格的事件一样快。如果有可能,请告诉我如何操作,如果没有,那么有什么替代方法呢?

Please Help Me.....

请帮我.....

Thanks a lot....

非常感谢....

回答by Matthew S

I know this may be a little late to the party, but this might be useful to someone else down the road.

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

In your MyView.xaml:

在您的 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>

In your MyView.xaml.cs:

在您的 MyView.xaml.cs 中:

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

回答by Anand Murali

An alternative way would to be define a DataGridTemplateColumninstead of using the predefined columns like DataGridCheckBoxColumn, DataGridComboBoxColumnand then add an event handler to the UI element defined in the data template.

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

Below I have defined a MouseDownevent handler for a TextBlockCell.

下面我MouseDownTextBlockCell定义了一个事件处理程序。

<DataGrid AutoGenerateColumns="False">
    <DataGrid.Columns>

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

In the Code behind file:

在代码隐藏文件中:

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

回答by Ε Г И ? И О

I know coding WPF is sometimes a PITA. Here you would have to handle the MouseDoubleClickevent anyway. Then search the source object hierarchy to find a DataGridRowand do whatever with it.

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

UPDATE: Sample code

更新:示例代码

XAML

XAML

<dg:DataGrid MouseDoubleClick="OnDoubleClick" />

Code behind

背后的代码

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.
}

}

}