即使在新项目源之后,WPF Datagrid 似乎仍保留验证错误引用

ali*_*sce 6 c# wpf datagrid

我观察到一个与 WPF DataGrid 验证相关的非常奇怪的问题,该问题仅在某些情况下发生。为了重现这个问题,我有一个测试窗口。

<Window x:Class="TEST.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:TEST"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"></RowDefinition>
            <RowDefinition Height="Auto">   
            </RowDefinition>
        </Grid.RowDefinitions>
        <DataGrid x:Name="datagrid" AutoGenerateColumns="True" Grid.Row="0" ColumnWidth="*"></DataGrid>
        <Button Click="Button_Click" Grid.Row="1">Fill Data</Button>
    </Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)

这是背后的代码:

namespace TEST
{
     
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        ObservableCollection<Person> list = null;
        private void Button_Click(object sender, RoutedEventArgs e)
        {
         list=   new ObservableCollection<Person>();
           
            for (int i = 0; i < 100; i++)
            {
                list.Add(new Person() { Age = 20, Name = "Person"+i.ToString()});
            }
            datagrid.ItemsSource = list;
        }
    }
    public class Person
    {
        public string Name { get; set; }
        public int Age { get; set; }
      
    }
}
Run Code Online (Sandbox Code Playgroud)

当我在“年龄”列下的单元格中写入非数值时,该单元格会出现红色边框以显示错误。但如果不更正该错误,如果我用新列表填充 DataGrid,错误就会按预期消失。现在,如果我输入 Age 单元格并右键单击,然后在关闭组合框之前单击另一行,则会引发异常:

System.ArgumentNullException: '值不能为空。参数名称:element' 此异常最初是在此调用堆栈中引发的:System.Windows.Automation.Peers.UIElementAutomationPeer.FromElement(System.Windows.UIElement) System.Windows.Controls.DataGrid.CellAutomationValueHolder.TrackValue() System.Windows。 Controls.DataGrid.ReleaseCellAutomationValueHolders() System.Windows.Controls.DataGrid.OnExecutedCommitEdit(System.Windows.Input.ExecutedRoulatedEventArgs) System.Windows.Controls.DataGrid.OnExecutedCommitEdit(object, System.Windows.Input.ExecutedRoulatedEventArgs) System.Windows.Input .CommandBinding.OnExecuted(对象,System.Windows.Input.ExecutedRoulatedEventArgs)System.Windows.Input.CommandManager.ExecuteCommandBinding(对象,System.Windows.Input.ExecutedRoulatedEventArgs,System.Windows.Input.CommandBinding)System.Windows.Input.CommandManager .FindCommandBinding(对象,System.Windows.RoulatedEventArgs,System.Windows.Input.ICommand,布尔)System.Windows.Input.CommandManager.OnExecuted(对象,System.Windows.Input.ExecutedRoulatedEventArgs)System.Windows.UIElement.OnExecutedThunk(对象、System.Windows.Input.ExecutedRoulatedEventArgs)

堆栈跟踪没有有关该问题的明确信息,除了上面的测试之外,其他一些单元格单击/编辑也可能会引发该异常。共同点是 WPF DataGrid 倾向于保留对先前 ItemSource 对象的引用,尤其是在存在验证错误时。我尝试过使用其他 IEnumerable 类型并将 ObservableCollection 定义移动到按钮单击事件中,但结果是相同的。

如何清除验证错误引用并使 DataGrid 忘记以前的 ItemSource 对象?

谢谢。