gpt4 book ai didi

c# - 在 WPF DataGrid 中单击编辑

转载 作者:IT王子 更新时间:2023-10-29 03:37:53 25 4
gpt4 key购买 nike

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

我如何覆盖或实现它?

最佳答案

我是这样解决这个问题的:

<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 绑定(bind)到 CollectionViewSource(包含虚拟 Person 对象)。

神奇之处在于:DataGridCell.Selected="DataGridCell_Selected"

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

这是事件处理程序的隐藏代码:

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

关于c# - 在 WPF DataGrid 中单击编辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3426765/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com