简体   繁体   English

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

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

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.所以,我想用 WPF DataGrid 做与 windows gridview 一样的事情。 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.他们中的一些人说使用 MouseDoubleClick 事件,但在这种情况下,我们必须检查每一行以及该行中的项目,因此检查每个单元格的数据很耗时,时间在这里最重要。

My DataGrid is bounded to DataTable and AutoGeneratedColumn is False.我的 DataGrid 绑定到 DataTable 并且 AutoGeneratedColumn 为 False。 If your answer is based on AutoGeneratedColumn=True then it is not possible.如果您的答案基于 AutoGeneratedColumn=True 那么这是不可能的。 Even, i 'm changing the styles of datagrid cell according to data, so there is no way to change AutoGeneratedColumn property.甚至,我正在根据数据更改数据网格单元格的样式,因此无法更改 AutoGeneratedColumn 属性。

A Cell Clicking/Double Clicking event should be as faster as windows grid's event.单元格单击/双击事件应该与 Windows 网格的事件一样快。 If it is possible then tell me how, and if not, then what is the alternative to do it?如果有可能,请告诉我如何操作,如果没有,那么有什么替代方法呢?

Please Help Me.....请帮我.....

Thanks a lot....非常感谢....

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
}

An alternative way would to be define a DataGridTemplateColumn instead of using the predefined columns like DataGridCheckBoxColumn , DataGridComboBoxColumn and then add an event handler to the UI element defined in the data template.另一种方法是定义DataGridTemplateColumn而不是使用预定义的列(如DataGridCheckBoxColumnDataGridComboBoxColumn ,然后将事件处理程序添加到数据模板中定义的 UI 元素。

Below I have defined a MouseDown event handler for a TextBlock Cell.下面我为TextBlock Cell 定义了一个MouseDown事件处理程序。

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

I know coding WPF is sometimes a PITA.我知道编码 WPF 有时是一个 PITA。 Here you would have to handle the MouseDoubleClick event anyway.在这里,您无论如何都必须处理MouseDoubleClick事件。 Then search the source object hierarchy to find a DataGridRow and do whatever with it.然后搜索源对象层次结构以找到DataGridRow并对其执行任何操作。

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

} }

I have used something like this:我用过这样的东西:

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

And handle my commands in my View Model.并在我的视图模型中处理我的命令。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM