gpt4 book ai didi

WPF DataGrid - 如何自动退出编辑模式?

转载 作者:行者123 更新时间:2023-12-04 01:06:30 29 4
gpt4 key购买 nike

我已经实现了 WPF DataGrid Single-Click Editing来自 Codeplex。
在该解决方案中,单击的单元格被聚焦并选择行以实现
DataGrid的单击编辑。效果很好。

这是代码:

private void DataGridCell_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
DataGridCell cell = sender as DataGridCell;
if (cell != null && !cell.IsEditing && !cell.IsReadOnly)
{
if (!cell.IsFocused)
{
cell.Focus();
}
DataGrid dataGrid = FindVisualParent<DataGrid>(cell);
if (dataGrid != null)
{
if (dataGrid.SelectionUnit != DataGridSelectionUnit.FullRow)
{
if (!cell.IsSelected)
cell.IsSelected = true;
}
else
{
DataGridRow row = FindVisualParent<DataGridRow>(cell);
if (row != null && !row.IsSelected)
{
row.IsSelected = true;
}
}
}
}
}

但我也希望我的 DataGrid 自动退出编辑模式(不按 Enter 键)
当单元格值更改时。例如,在编辑模式下,我在单元格中有一个组合框。当用户在组合框中选择一个值时,它将自动对所选值进行数据绑定(bind)。但是用户仍然需要单击 Enter 退出编辑模式。如何自动退出编辑模式?

我已经尝试监听属性更改并调用 DataGrid 的 CommitEdit 函数以自动退出编辑模式。效果很好,代码如下:
 void _gameCompareViewModel_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
if (e.PropertyName == "End Edit")
{
AlignGrid.CommitEdit();
}

}

但是现在单击编辑功能不适用于当前单元格。
我必须先单击不同的行才能使其工作。
我想我想要的是当调用 CommmitEdit 时,它会自动选择一个
不同的行。 (就像当你按 Enter 时,它会转到下一行)
有什么建议吗?请告诉我如何执行此操作的代码。我的项目没时间了。

谢谢您的帮助。

最佳答案

从单元格编辑模板翻转回单元格模板:

dataGrid.CancelEdit();

关于WPF DataGrid - 如何自动退出编辑模式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4880270/

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