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

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 this issue Sep 1, 2023 · 3 comments
Labels

Comments

@ArsenAbazian
Copy link

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
Copy link
Contributor

timunie commented Sep 1, 2023

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

@timunie
Copy link
Contributor

timunie commented Sep 4, 2023

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

  • messing with the UI
  • Hard to maintain in the long term
  • Using the ApplicationLifetime which makes it hard to move to another OS later on

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.

@timunie timunie removed the bug label Sep 4, 2023
@AvaloniaUI AvaloniaUI locked and limited conversation to collaborators Sep 4, 2023
@timunie timunie converted this issue into a discussion Sep 4, 2023
@kekekeks kekekeks reopened this Sep 4, 2023
@kekekeks
Copy link
Member

kekekeks commented Sep 4, 2023

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

@kekekeks kekekeks added the bug label Sep 4, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

3 participants