我如何处理WPF DataGrid上的单元格双击事件,相当于Windows DataGrid的事件? [英] How do i handle cell double click event on WPF DataGrid, equivalent to windows DataGrid's Events?

查看:273
本文介绍了我如何处理WPF DataGrid上的单元格双击事件,相当于Windows DataGrid的事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如您所知,在Windows C#的gridview中,如果要处理单元格上的点击/双击事件,则会出现像CellClick,CellDoubleClick等事件。



所以,我想像WPF DataGrid的windows gridview那样做。我迄今已经搜索过,但答案都不适用,也不实用。他们中的一些人说使用MouseDoubleClick事件,但在这种情况下,我们必须检查该行中的每一行以及该行中的项目,因此检查每个单元格数据是非常耗时的,时序在这里是最重要的。 >

我的DataGrid绑定到DataTable,AutoGeneratedColumn为False。如果您的答案基于AutoGeneratedColumn = True,那么这是不可能的。即使我根据数据更改datagrid单元格的样式,所以没有办法更改AutoGeneratedColumn属性。



单元格单击事件应为像windows网格事件一样快。如果可能的话可以告诉我怎么做,如果没有,那么可以做什么呢?



请帮助我.....



感谢很多....

解决方案

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



下面我已经定义了一个 MouseDown TextBlock 单元格的事件处理程序

 < DataGrid AutoGenerateColumns =假> 
< 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)
{
//某些逻辑
// block.Text
}
}


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.

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.

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.

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?

Please Help Me.....

Thanks a lot....

解决方案

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.

Below I have defined a MouseDown event handler for a TextBlock Cell.

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

这篇关于我如何处理WPF DataGrid上的单元格双击事件,相当于Windows DataGrid的事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆