DataGridCheckBoxColumn的用法—引发未处理的异常

在DataGrid中使用DataGridCheckBoxColumn并将其绑定到支持模型中的布尔属性时,会发生这种情况。单击该列并选中/未选中复选框时,它的行为与预期的一样,但是当我关闭主窗口时,将引发异常。仅使用文本列并与之交互时不会发生这种情况。如果我根本不与复选框列进行交互并且仅关闭窗口,也不会发生这种情况。只有在与复选框列进行交互之后关闭窗口时,才会发生这种情况。

开发环境:

作业系统: 1. Arch Linux,Gnome 3和Wayland。 2. Windows 10专业版

开发人员: dotnet核心3.1.103 IDE:VS代码

重现步骤:

General procedure to use a DataGrid was that mentioned in the tutorial article: Avalonia DataGrid Getting Started

我对本文所做的唯一更改是将DataGridCheckBoxColumn添加到DataGrid,并将布尔IsChecked属性添加到本教程中使用的“ Person”模型类中-然后将两者绑定在一起。

因此关键步骤是:

  1. $ dotnet new avalonia.mvvm
  2. In the *csproj file, knock up the versions of all Avalonia includes to 0.9.10 as thats the version of the DataGrid I'm using.
  3. $ dotnet add package Avalonia.Controls.DataGrid
  4. In the App.xaml , add the style:
    <StyleInclude Source="avares://Avalonia.Controls.DataGrid/Themes/Default.xaml"/>
  5. Add the following property to the model class "Person" --- rest of the the code is untouched and is as per the linked tutorial:
 public bool IsChecked
        {get;set;}
  1. 将DataGridCheckBoxColumn添加到DataGrid并按如下所示绑定它(其余的列保持不变,并按照链接的教程进行操作):
<DataGrid.Columns>
        <DataGridTextColumn Header="First Name" 
                            Binding="{Binding FirstName}" 
                            Width="2*" />
        <DataGridTextColumn Header="Last Name" 
                            Binding="{Binding LastName}" 
                            Width="2*" />
        <DataGridTextColumn Header="Department" 
                            Binding="{Binding DepartmentNumber}" 
                            Width="*" />

        <DataGridCheckBoxColumn Header="IsChecked" 
                            Binding="{Binding IsChecked}" 
                            Width="*" />

    </DataGrid.Columns>
  1. 然后,当我运行该应用程序(从命令行或vscode)时-GUI会按预期显示-带有所有列和值-并且通常会显示其他复选框列。 __
  2. 当我单击复选框列时-复选框确实按预期方式被选中和未选中(尽管需要额外单击一次)。但是,当我单击右上角的“ X”关闭窗口时-发生未处理的异常-不在此之前。

完整的异常文本:

未处理的异常。 System.InvalidOperationException:集合已修改;枚举操作可能无法执行。    在System.Collections.Generic.List`1.Enumerator.MoveNextRare()处    在Avalonia.Collections.AvaloniaList`1.Enumerator.MoveNext()    在System.Linq.Enumerable.OfTypeIterator [TResult](IEnumerable源)+ MoveNext()    在Avalonia.Visual.OnDetachedFromVisualTreeCore(VisualTreeAttachmentEventArgs e)    在Avalonia.Input.InputElement.OnDetachedFromVisualTreeCore(VisualTreeAttachmentEventArgs e)    在Avalonia.Controls.Control.OnDetachedFromVisualTreeCore(VisualTreeAttachmentEventArgs e)    在Avalonia.Visual.OnDetachedFromVisualTreeCore(VisualTreeAttachmentEventArgs e)    在Avalonia.Input.InputElement.OnDetachedFromVisualTreeCore(VisualTreeAttachmentEventArgs e)    在Avalonia.Controls.Control.OnDetachedFromVisualTreeCore(VisualTreeAttachmentEventArgs e)    在Avalonia.Visual.OnDetachedFromVisualTreeCore(VisualTreeAttachmentEventArgs e)    在Avalonia.Input.InputElement.OnDetachedFromVisualTreeCore(VisualTreeAttachmentEventArgs e)    在Avalonia.Controls.Control.OnDetachedFromVisualTreeCore(VisualTreeAttachmentEventArgs e)    在Avalonia.Visual.OnDetachedFromVisualTreeCore(VisualTreeAttachmentEventArgs e)    在Avalonia.Input.InputElement.OnDetachedFromVisualTreeCore(VisualTreeAttachmentEventArgs e)    在Avalonia.Controls.Control.OnDetachedFromVisualTreeCore(VisualTreeAttachmentEventArgs e)    在Avalonia.Visual.OnDetachedFromVisualTreeCore(VisualTreeAttachmentEventArgs e)    在Avalonia.Input.InputElement.OnDetachedFromVisualTreeCore(VisualTreeAttachmentEventArgs e)    在Avalonia.Controls.Control.OnDetachedFromVisualTreeCore(VisualTreeAttachmentEventArgs e)    在Avalonia.Visual.OnDetachedFromVisualTreeCore(VisualTreeAttachmentEventArgs e)    在Avalonia.Input.InputElement.OnDetachedFromVisualTreeCore(VisualTreeAttachmentEventArgs e)    在Avalonia.Controls.Control.OnDetachedFromVisualTreeCore(VisualTreeAttachmentEventArgs e)    在Avalonia.Visual.OnDetachedFromVisualTreeCore(VisualTreeAttachmentEventArgs e)    在Avalonia.Input.InputElement.OnDetachedFromVisualTreeCore(VisualTreeAttachmentEventArgs e)    在Avalonia.Controls.Control.OnDetachedFromVisualTreeCore(VisualTreeAttachmentEventArgs e)    在Avalonia.Visual.OnDetachedFromVisualTreeCore(VisualTreeAttachmentEventArgs e)    在Avalonia.Input.InputElement.OnDetachedFromVisualTreeCore(VisualTreeAttachmentEventArgs e)    在Avalonia.Controls.Control.OnDetachedFromVisualTreeCore(VisualTreeAttachmentEventArgs e)    在Avalonia.Visual.OnDetachedFromVisualTreeCore(VisualTreeAttachmentEventArgs e)    在Avalonia.Input.InputElement.OnDetachedFromVisualTreeCore(VisualTreeAttachmentEventArgs e)    在Avalonia.Controls.Control.OnDetachedFromVisualTreeCore(VisualTreeAttachmentEventArgs e)    在Avalonia.Visual.OnDetachedFromVisualTreeCore(VisualTreeAttachmentEventArgs e)    在Avalonia.Input.InputElement.OnDetachedFromVisualTreeCore(VisualTreeAttachmentEventArgs e)    在Avalonia.Controls.Control.OnDetachedFromVisualTreeCore(VisualTreeAttachmentEventArgs e)    在Avalonia.Visual.OnDetachedFromVisualTreeCore(VisualTreeAttachmentEventArgs e)    在Avalonia.Input.InputElement.OnDetachedFromVisualTreeCore(VisualTreeAttachmentEventArgs e)    在Avalonia.Controls.Control.OnDetachedFromVisualTreeCore(VisualTreeAttachmentEventArgs e)    在Avalonia.Visual.OnDetachedFromVisualTreeCore(VisualTreeAttachmentEventArgs e)    在Avalonia.Input.InputElement.OnDetachedFromVisualTreeCore(VisualTreeAttachmentEventArgs e)    在Avalonia.Controls.Control.OnDetachedFromVisualTreeCore(VisualTreeAttachmentEventArgs e)    在Avalonia.Visual.OnDetachedFromVisualTreeCore(VisualTreeAttachmentEventArgs e)    在Avalonia.Input.InputElement.OnDetachedFromVisualTreeCore(VisualTreeAttachmentEventArgs e)    在Avalonia.Controls.Control.OnDetachedFromVisualTreeCore(VisualTreeAttachmentEventArgs e)    在Avalonia.Visual.OnDetachedFromVisualTreeCore(VisualTreeAttachmentEventArgs e)    在Avalonia.Input.InputElement.OnDetachedFromVisualTreeCore(VisualTreeAttachmentEventArgs e)    在Avalonia.Controls.Control.OnDetachedFromVisualTreeCore(VisualTreeAttachmentEventArgs e)    在Avalonia.Visual.OnDetachedFromVisualTreeCore(VisualTreeAttachmentEventArgs e)    在Avalonia.Input.InputElement.OnDetachedFromVisualTreeCore(VisualTreeAttachmentEventArgs e)    在Avalonia.Controls.Control.OnDetachedFromVisualTreeCore(VisualTreeAttachmentEventArgs e)    在Avalonia.Controls.TopLevel.HandleClosed()    在Avalonia.Controls.WindowBase.HandleClosed()    在Avalonia.Controls.Window.HandleClosed()    在Avalonia.X11.X11Window.Cleanup()    在Avalonia.X11.X11Window.Dispose()    在Avalonia.X11.X11Window.OnEventSync(XEvent ev)    在Avalonia.X11.X11Window.OnEvent(XEvent ev)    在Avalonia.X11.X11PlatformThreading.HandleX11(CancellationToken cancellingToken)    在Avalonia.X11.X11PlatformThreading.RunLoop(CancellationToken cancelledToken)    在Avalonia.Threading.Dispatcher.MainLoop(CancellationToken cancelleToken)    在Avalonia.Controls.ApplicationLifetimes.ClassicDesktopStyleApplicationLifetime.Start(String [] args)    在Avalonia.ClassicDesktopStyleApplicationLifetimeExtensions.StartWithClassicDesktopLifetime [T](T builder,String [] args,ShutdownMode shutdownMode)    在/home/[myusername]/[pathtoproject]/Program.cs:line 14中的d3.Program.Main(String [] args)