当其中一个单元格无效时,如何编辑wpfdatagrid其他单元格? [英] How to edit wpfdatagrid other cells while one of it's cell is invalid?
本文介绍了当其中一个单元格无效时,如何编辑wpfdatagrid其他单元格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在 WPF数据网格
中,当一个单元格无效时,它将阻止其他单元格的编辑,因此用户必须在无效单元格变为有效之前才能输入数据。我想知道是否有一种方法可以禁用此行为?
In WPF datagrid
,When a cell is invalid,it prevents the the other cells editing so user can not enter data until the invalid cell comes valid.I was wonder if there is a way to disable this behavior?
我是如何使用 datagrid
:
<DataGrid ItemsSource="{Binding ..}">
<DataGrid.Columns>
<DataGridTextColumn Header="Name"
Binding="{Binding Name
, UpdateSourceTrigger=PropertyChanged
, NotifyOnValidationError=True
, ValidatesOnDataErrors=True
, ValidatesOnExceptions=True}"
</DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
推荐答案
我覆盖了<$ c $的OnCanExecuteBeginEdit方法c> datagrid 如下所示,并且现在可以正常工作。
I overrided the OnCanExecuteBeginEdit method of the datagrid
like below and it works now.
protected override void OnCanExecuteBeginEdit(System.Windows.Input.CanExecuteRoutedEventArgs e)
{
var hasCellValidationError = false;
var hasRowValidationError = false;
const BindingFlags bindingFlags =
BindingFlags.FlattenHierarchy | BindingFlags.NonPublic | BindingFlags.Instance;
var cellError= this.GetType().BaseType.GetProperty("HasCellValidationError", bindingFlags);
var rowError = this.GetType().BaseType.GetProperty("HasRowValidationError", bindingFlags);
if (cellError != null)
hasCellValidationError = (bool) cellErrorInfo.GetValue(this, null);
if (rowError != null)
hasRowValidationError = (bool) rowErrorInfo.GetValue(this, null);
base.OnCanExecuteBeginEdit(e);
if ((!e.CanExecute && hasCellValidationError) || (!e.CanExecute && hasRowValidationError))
{
e.CanExecute = true;
e.Handled = true;
}
}
相同的问题:DataGrid:在单元格验证错误上,其他行单元格不可编辑/只读
这篇关于当其中一个单元格无效时,如何编辑wpfdatagrid其他单元格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文