Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DataGridCheckBoxColumn usage -- throws an Unhandled exception. #3973

Open
explorest opened this issue May 21, 2020 · 9 comments
Open

DataGridCheckBoxColumn usage -- throws an Unhandled exception. #3973

explorest opened this issue May 21, 2020 · 9 comments

Comments

@explorest
Copy link

explorest commented May 21, 2020

This happens when DataGridCheckBoxColumn is used in a DataGrid and is bound to a boolean property in the backing model. When the column is clicked and the check boxes checked/unchecked -- it behaves as expected -- but then when I close the main window, the exception is thrown. This does not happen when only text columns are used and interacted with. It also doesn't happen if I do NOT interact with the checkbox column at all and just close the window. It only happens when I close the window AFTER I have interacted with the check box column.

Development environment:

OSes:

  1. Arch Linux , Gnome 3 , Wayland.
  2. Windows 10 Pro

Dev: dotnet core 3.1.103
IDE: VS Code

Steps to reproduce:

General procedure to use a DataGrid was that mentioned in the tutorial article:
Avalonia DataGrid getting started

The only key change I made to the article was adding a DataGridCheckBoxColumn to the DataGrid and adding a boolean IsChecked property to the "Person" model class used in the tutorial -- and then bind the two together.

So the key steps are:

  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. Add the DataGridCheckBoxColumn to the DataGrid and bind it as shown below (rest of the columns are untouched and are as per the linked tutorial):
<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. Then when I run the application (either from command line or vscode) -- the GUI shows up as expected-- with all the columns and values -- and the additional checkbox column is normally shown. __
    When I click in the checkboxes column -- the check boxes do get checked and unchecked as expected (although one extra click is required). But then when I close the window by clicking on the "X" on right upper corner -- that's when the unhandled exception happens -- NOT before that.

The complete exception text:

Unhandled exception. System.InvalidOperationException: Collection was modified; enumeration operation may not execute.
at System.Collections.Generic.List1.Enumerator.MoveNextRare() at Avalonia.Collections.AvaloniaList1.Enumerator.MoveNext()
at System.Linq.Enumerable.OfTypeIterator[TResult](IEnumerable source)+MoveNext()
at Avalonia.Visual.OnDetachedFromVisualTreeCore(VisualTreeAttachmentEventArgs e)
at Avalonia.Input.InputElement.OnDetachedFromVisualTreeCore(VisualTreeAttachmentEventArgs e)
at Avalonia.Controls.Control.OnDetachedFromVisualTreeCore(VisualTreeAttachmentEventArgs e)
at Avalonia.Visual.OnDetachedFromVisualTreeCore(VisualTreeAttachmentEventArgs e)
at Avalonia.Input.InputElement.OnDetachedFromVisualTreeCore(VisualTreeAttachmentEventArgs e)
at Avalonia.Controls.Control.OnDetachedFromVisualTreeCore(VisualTreeAttachmentEventArgs e)
at Avalonia.Visual.OnDetachedFromVisualTreeCore(VisualTreeAttachmentEventArgs e)
at Avalonia.Input.InputElement.OnDetachedFromVisualTreeCore(VisualTreeAttachmentEventArgs e)
at Avalonia.Controls.Control.OnDetachedFromVisualTreeCore(VisualTreeAttachmentEventArgs e)
at Avalonia.Visual.OnDetachedFromVisualTreeCore(VisualTreeAttachmentEventArgs e)
at Avalonia.Input.InputElement.OnDetachedFromVisualTreeCore(VisualTreeAttachmentEventArgs e)
at Avalonia.Controls.Control.OnDetachedFromVisualTreeCore(VisualTreeAttachmentEventArgs e)
at Avalonia.Visual.OnDetachedFromVisualTreeCore(VisualTreeAttachmentEventArgs e)
at Avalonia.Input.InputElement.OnDetachedFromVisualTreeCore(VisualTreeAttachmentEventArgs e)
at Avalonia.Controls.Control.OnDetachedFromVisualTreeCore(VisualTreeAttachmentEventArgs e)
at Avalonia.Visual.OnDetachedFromVisualTreeCore(VisualTreeAttachmentEventArgs e)
at Avalonia.Input.InputElement.OnDetachedFromVisualTreeCore(VisualTreeAttachmentEventArgs e)
at Avalonia.Controls.Control.OnDetachedFromVisualTreeCore(VisualTreeAttachmentEventArgs e)
at Avalonia.Visual.OnDetachedFromVisualTreeCore(VisualTreeAttachmentEventArgs e)
at Avalonia.Input.InputElement.OnDetachedFromVisualTreeCore(VisualTreeAttachmentEventArgs e)
at Avalonia.Controls.Control.OnDetachedFromVisualTreeCore(VisualTreeAttachmentEventArgs e)
at Avalonia.Visual.OnDetachedFromVisualTreeCore(VisualTreeAttachmentEventArgs e)
at Avalonia.Input.InputElement.OnDetachedFromVisualTreeCore(VisualTreeAttachmentEventArgs e)
at Avalonia.Controls.Control.OnDetachedFromVisualTreeCore(VisualTreeAttachmentEventArgs e)
at Avalonia.Visual.OnDetachedFromVisualTreeCore(VisualTreeAttachmentEventArgs e)
at Avalonia.Input.InputElement.OnDetachedFromVisualTreeCore(VisualTreeAttachmentEventArgs e)
at Avalonia.Controls.Control.OnDetachedFromVisualTreeCore(VisualTreeAttachmentEventArgs e)
at Avalonia.Visual.OnDetachedFromVisualTreeCore(VisualTreeAttachmentEventArgs e)
at Avalonia.Input.InputElement.OnDetachedFromVisualTreeCore(VisualTreeAttachmentEventArgs e)
at Avalonia.Controls.Control.OnDetachedFromVisualTreeCore(VisualTreeAttachmentEventArgs e)
at Avalonia.Visual.OnDetachedFromVisualTreeCore(VisualTreeAttachmentEventArgs e)
at Avalonia.Input.InputElement.OnDetachedFromVisualTreeCore(VisualTreeAttachmentEventArgs e)
at Avalonia.Controls.Control.OnDetachedFromVisualTreeCore(VisualTreeAttachmentEventArgs e)
at Avalonia.Visual.OnDetachedFromVisualTreeCore(VisualTreeAttachmentEventArgs e)
at Avalonia.Input.InputElement.OnDetachedFromVisualTreeCore(VisualTreeAttachmentEventArgs e)
at Avalonia.Controls.Control.OnDetachedFromVisualTreeCore(VisualTreeAttachmentEventArgs e)
at Avalonia.Visual.OnDetachedFromVisualTreeCore(VisualTreeAttachmentEventArgs e)
at Avalonia.Input.InputElement.OnDetachedFromVisualTreeCore(VisualTreeAttachmentEventArgs e)
at Avalonia.Controls.Control.OnDetachedFromVisualTreeCore(VisualTreeAttachmentEventArgs e)
at Avalonia.Visual.OnDetachedFromVisualTreeCore(VisualTreeAttachmentEventArgs e)
at Avalonia.Input.InputElement.OnDetachedFromVisualTreeCore(VisualTreeAttachmentEventArgs e)
at Avalonia.Controls.Control.OnDetachedFromVisualTreeCore(VisualTreeAttachmentEventArgs e)
at Avalonia.Visual.OnDetachedFromVisualTreeCore(VisualTreeAttachmentEventArgs e)
at Avalonia.Input.InputElement.OnDetachedFromVisualTreeCore(VisualTreeAttachmentEventArgs e)
at Avalonia.Controls.Control.OnDetachedFromVisualTreeCore(VisualTreeAttachmentEventArgs e)
at Avalonia.Controls.TopLevel.HandleClosed()
at Avalonia.Controls.WindowBase.HandleClosed()
at Avalonia.Controls.Window.HandleClosed()
at Avalonia.X11.X11Window.Cleanup()
at Avalonia.X11.X11Window.Dispose()
at Avalonia.X11.X11Window.OnEventSync(XEvent ev)
at Avalonia.X11.X11Window.OnEvent(XEvent ev)
at Avalonia.X11.X11PlatformThreading.HandleX11(CancellationToken cancellationToken)
at Avalonia.X11.X11PlatformThreading.RunLoop(CancellationToken cancellationToken)
at Avalonia.Threading.Dispatcher.MainLoop(CancellationToken cancellationToken)
at Avalonia.Controls.ApplicationLifetimes.ClassicDesktopStyleApplicationLifetime.Start(String[] args)
at Avalonia.ClassicDesktopStyleApplicationLifetimeExtensions.StartWithClassicDesktopLifetime[T](T builder, String[] args, ShutdownMode shutdownMode)
at d3.Program.Main(String[] args) in /home/[myusername]/[pathtoproject]/Program.cs:line 14

@explorest explorest changed the title DataGridCheckBoxColumn throws Unhandled exception DataGridCheckBoxColumn throws Unhandled exception after it is interacted with and then the main windows closed. May 21, 2020
@explorest explorest changed the title DataGridCheckBoxColumn throws Unhandled exception after it is interacted with and then the main windows closed. DataGridCheckBoxColumn throws Unhandled exception after it is interacted with and then the MainWindow closed. May 21, 2020
@explorest explorest changed the title DataGridCheckBoxColumn throws Unhandled exception after it is interacted with and then the MainWindow closed. DataGridCheckBoxColumn usage-- after the checkboxes are interacted with and then the MainWindow closed -- throws an Unhandled exception. May 21, 2020
@explorest explorest changed the title DataGridCheckBoxColumn usage-- after the checkboxes are interacted with and then the MainWindow closed -- throws an Unhandled exception. DataGridCheckBoxColumn usage -- throws an Unhandled exception. May 22, 2020
@explorest
Copy link
Author

Reproduced on Windows 10 as well -- so it has nothing to do with the underlying OS or windowing system.

@JamRemco
Copy link
Contributor

Hello world,

The link you provided is wrong (found the article nevertheless)
Did you tried latest build ,09.9 i think

Regards,

Remco

@explorest
Copy link
Author

explorest commented May 22, 2020

Thanks for the reply. I have corrected the link.
I tried it on 0.9.10 --- all packages on this version. Following is the relevant part of my project file:

<ItemGroup>
    <PackageReference Include="Avalonia" Version="0.9.10" />
    <PackageReference Include="Avalonia.Controls.DataGrid" Version="0.9.10" />
    <PackageReference Include="Avalonia.Desktop" Version="0.9.10" />
    <PackageReference Include="Avalonia.ReactiveUI" Version="0.9.10" />
  </ItemGroup>

@JamRemco
Copy link
Contributor

JamRemco commented May 22, 2020

mmmmm.
Works on my machine -win 10
that extra click is for the 3 state and selecting the column
Edit
Hey somethimes i got an exception to. thats odd i got the idea that the chanhes are not comitted properly to the collection.

If you run the app without the changes you made, does it run then properly?
Edit
Regards

Remco

@explorest
Copy link
Author

explorest commented May 22, 2020

Thanks for the reply. For the time being I have given up on finding a solution. Maybe it will go away in the future. On the extra click thing , I have read somewhere on the net that the first click is just for selecting the checkbox (and the second click is for actually checking/unchecking) -- while the three valued checkbox looks very different (I've tried that as well).
Meanwhile I will settle for the following DataGridTemplateColumn based solution/workaround with embedded regular checkbox -- which works for me without throwing any exceptions, and actually has the nice side effect of the checkbox being checked/unchecked on a single click -- solving both my issues in one masterstroke.
So I'm happy for now :)

<DataGridTemplateColumn Header="Check/Uncheck">
           <DataGridTemplateColumn.CellTemplate>
               <DataTemplate>
                   <CheckBox IsChecked="{Binding IsChecked}"/>
               </DataTemplate>
           </DataGridTemplateColumn.CellTemplate>
       </DataGridTemplateColumn>

@JamRemco
Copy link
Contributor

if thats works than that is a reasenable solution, actually, i was at te point to advice a datatemplete which this basicly is.

Stil dont know why it fails i made several datagrid test project with succces

Mabe the wrong version (0.9.1 is a bit old) latest version now is 0.9.9 i believe.
What you could try is to nuget the latest version

Regards,

Remco

@explorest
Copy link
Author

Its the latest version. It is 0.9.10 ( and NOT 0.9.1 ).

@JamRemco
Copy link
Contributor

Its the latest version. It is 0.9.10 ( and NOT 0.9.1 ).

Yeay your right, my bad.
I did read that wrong.
I thought the version after 0.9.9 would be 1.0.0 not 09.9.10
Silly me ;)

@NoralK
Copy link

NoralK commented Aug 28, 2020

This is also happening with 0.9.12. However, if I put an Exit button and call this.Close() the exception is not thrown.

@danwalmsley Is this being looked at?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants