在WPF的DataGrid单一的点击编辑编辑、WPF、DataGrid

2023-09-02 11:49:48 作者:Dreamer梦想家

我希望用户能够把细胞进入编辑模式,并突出显示单元包含在该行与一个单一的点击。默认情况下,这是双击。我如何重写或实现这一点?我搜索谷歌,和codePLEX答案不为我工作。

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. How do I override or implement this? I've searched on google, and the codeplex answer doesn't work for me.

我一般pretty的新的WPF和编码,这样一个简单的答案比较好。

I'm pretty new to WPF and coding in general, so a simple answer is better.

推荐答案

下面是我如何解决这个问题:

Here is how I resolved this issue:

<DataGrid DataGridCell.Selected="DataGrid_GotFocus" 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(含虚拟的人对象)。

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

神奇的发生有: DataGridCell.Selected =DataGrid_GotFocus

我只是勾DataGrid单元格的选定事件,并在DataGrid中调用BeginEdit()。

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

下面是为后面的事件处理程序的code:

Here is the code behind for the event handler :

    private void DataGrid_GotFocus(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);
        }
    }

{}欣赏