AvaloniaUI / Avalonia

Develop Desktop, Embedded, Mobile and WebAssembly apps with C# and XAML. The most popular .NET UI client technology
https://avaloniaui.net
MIT License
26.09k stars 2.26k forks source link

Attempt to call InvalidateArrange on wrong LayoutManager exception is get, when trying to move item from ItemsControl in one window to the ItemsContron in other window #12753

Open ArsenAbazian opened 1 year ago

ArsenAbazian commented 1 year ago

Describe the bug There are two similar windows with ItemsControl in them. In first window ItemsControl contains one item, for example: Button. In one moment the button is removed from the current ItemsControl.Items collection and added to the ItemsControl.Items in ItemsControl, located in other window. After that if you try to resize the first window, an "Attempt to call InvalidateArrange on wrong LayoutManager" exception will be thrown.

To Reproduce

  1. Create two windows (Window1, Window2), with ItemsControl in each window.
  2. In Window1 constructor create and add Button1 button to Window1.ItemsControl.
  3. Add button (Button2) to Window1 and handle click event.
  4. On Button2.Click event handler write code to remove Button1 from Window1.ItemsControl and add Button1 to Window2.ItemsControl
  5. Resize Window1
  6. Exception is thrown

Expected behavior No exception is thrown, Button1 successfully appear in Window2 and disappear from Window1

Desktop (please complete the following information): OS: Windows 11 Avalonia version: 11.0.4

Additional context I added the sample project, demonstating the issue. Please run the sample project and press the "Move Item To Main Window Container" button in FloatingWindow. Notice that button is not moved to MainWidow and stays in FloatingWindow. Then try to resize FloatingWindow. As a workaround try to call the UpdateLayout method after removing the button from first ItemsControl. AvaloniaApplication1.zip

Thank you, Arsen.

timunie commented 1 year ago

better to work with the MVVM approach and use ItemTemplates. Your items can then be added or removed as needed.

timunie commented 1 year ago

@ArsenAbazian Trying your sample I still think this is not a good idea to do in Avalonia. This approach has some drawbacks:

Attached please find an alternative, using the CommunityToolkit.Mvvm package AvaloniaApplication1.zip

Please visit https://github.com/AvaloniaUI/Avalonia.Samples for more samples and tutorials.

kekekeks commented 1 year ago

This is a legit bug. Cross-window control reparenting should be supported.