在 WPF DataGrid 中单击编辑 [英] Single click edit in WPF DataGrid

查看:57
本文介绍了在 WPF DataGrid 中单击编辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望用户能够将单元格置于编辑模式并通过单击突出显示单元格所在的行.默认情况下,这是双击.

I want the user to be able to put the cell into editing mode and highlight the row the cell is contained in with a single click. By default, this is double click.

我如何覆盖或实现它?

推荐答案

以下是我解决此问题的方法:

Here is how I resolved this issue:

<DataGrid DataGridCell.Selected="DataGridCell_Selected" 
          ItemsSource="{Binding Source={StaticResource itemView}}">
    <DataGrid.Columns>
        <DataGridTextColumn Header="Nom" Binding="{Binding Path=Name}"/>
        <DataGridTextColumn Header="Age" Binding="{Binding Path=Age}"/>
    </DataGrid.Columns>
</DataGrid>

此 DataGrid 绑定到 CollectionViewSource(包含虚拟 Person 对象).

This DataGrid is bound to a CollectionViewSource (Containing dummy Person objects).

神奇就在那里:DataGridCell.Selected="DataGridCell_Selected".

我只是挂钩 DataGrid 单元格的 Selected 事件,并在 DataGrid 上调用 BeginEdit().

I simply hook the Selected Event of the DataGrid cell, and call BeginEdit() on the DataGrid.

这是事件处理程序背后的代码:

Here is the code behind for the event handler :

private void DataGridCell_Selected(object sender, RoutedEventArgs e)
{
    // Lookup for the source to be DataGridCell
    if (e.OriginalSource.GetType() == typeof(DataGridCell))
    {
        // Starts the Edit on the row;
        DataGrid grd = (DataGrid)sender;
        grd.BeginEdit(e);
    }
}

这篇关于在 WPF DataGrid 中单击编辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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