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
25.33k stars 2.2k forks source link

TreeView's SelectedItem in a Flyout gets set to null when the Flyout is closed #14221

Open FawazTakahji opened 8 months ago

FawazTakahji commented 8 months ago

Describe the bug

If a top level item gets selected and the flyout gets closed the selecteditem gets set to null, this doesn't happen with a subitem

To Reproduce

  1. Put a TreeView in a Flyout
  2. Bind the SelectedItem to a property in the viewmodel
  3. Select a top level item and close the flyout
  4. SelectedItem will be set to null

Screenshots

AvaloniaBug_vzYmlZBe3X

Environment

Additional context

timunie commented 8 months ago

Maybe give #13970 a try?

FawazTakahji commented 8 months ago

Maybe give #13970 a try?

I gave it a try, by default it doesn't fix it, but if I set UpdateSourceTrigger to "LostFocus" it kinda fixes it AvaloniaBug_cO4XjyXF2F

goudcode commented 8 months ago

Same when navigating to a different page.

https://github.com/AvaloniaUI/Avalonia/assets/23435053/13f8aebc-9061-4ec9-bb9b-69ff06117b8d

I have a sample project attached where this issue is reproducible.

treeviewtest.zip

FawazTakahji commented 8 months ago

As a temporary fix setting the binding mode for the ItemsSource to onetime fixes this issue on 11.0.7

People that use CommunityToolkit.Mvvm can do something like this

partial void OnSelectedItemChanged(Item? oldValue, Item newValue)
{
    if (newValue == null)
    {
        Dispatcher.UIThread.InvokeAsync(async () =>
        {
            await Task.Delay(3);
            SelectedItem = oldValue ?? Items[0];
        });
    }
}

if the property gets changed without a delay it keeps trying to set it to null until a stack overflow exception happens