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

TreeView does not respect initial SelectedItem when initialized #10120

Open
AndrewKeepCoding opened this issue Oct 30, 2024 · 7 comments
Open
Labels
area-TreeView bug Something isn't working

Comments

@AndrewKeepCoding
Copy link
Contributor

AndrewKeepCoding commented Oct 30, 2024

Describe the bug

I created a reproducible project here but is something like the following:

public partial class ShellViewModel : ObservableObject
{
    [ObservableProperty]
    private string[] _items =
        [
            "Item 1",
            "Item 2",
            "Item 3",
        ];

    [ObservableProperty]
    private string? _selectedItem;

    public ShellViewModel()
    {
        SelectedItem = Items[1];
    }

    [RelayCommand]
    private void SelectFirstItem() => SelectedItem = Items.First();

    [RelayCommand]
    private void SelectLastItem() => SelectedItem = Items.Last();
}
<ListView
    ItemsSource="{x:Bind ViewModel.Items}"
    SelectedItem="{x:Bind ViewModel.SelectedItem, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
<GridView
    ItemsSource="{x:Bind ViewModel.Items}"
    SelectedItem="{x:Bind ViewModel.SelectedItem, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
<NavigationView
    MenuItemsSource="{x:Bind ViewModel.Items}"
    SelectedItem="{x:Bind ViewModel.SelectedItem, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
<TreeView
    ItemsSource="{x:Bind ViewModel.Items}"
    SelectedItem="{x:Bind ViewModel.SelectedItem, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />

As you can see, the TreeView control is not respecting the bound SelectedItem right after it's initialized. It should have "Item 2" selected. And you can also see that the binding is not broken.

20241030-1431-52.8086650.mp4

Steps to reproduce the bug

Use the repo above.

Expected behavior

TreeView respects SelectedItem when loaded.

Screenshots

No response

NuGet package version

WinUI 3 - Windows App SDK 1.6.1: 1.6.240923002

Windows version

Windows Insider Build (xxxxx)

Additional context

No response

@AndrewKeepCoding AndrewKeepCoding added the bug Something isn't working label Oct 30, 2024
@microsoft-github-policy-service microsoft-github-policy-service bot added the needs-triage Issue needs to be triaged by the area owners label Oct 30, 2024
@ghost1372
Copy link
Contributor

ghost1372 commented Oct 30, 2024

I think this is happening because the treeview control is not loaded yet!

Therefore, instead of setting the selectedItem in the constructor, set it in the page loaded event

@AndrewKeepCoding
Copy link
Contributor Author

Your sample project link is broken...

Sorry about that. Fixed the link.

I think this is happening because the treeview control is not loaded yet!
Therefore, instead of setting the selectedItem in the constructor, set it in the page loaded event

The SelectedItem is not set in the constructor, it's just bound to a property in the view model. ListView, GridView and NavigationView are under the same state but only the TreeView doesn't select the bound item.

@AndrewKeepCoding
Copy link
Contributor Author

I understand that they are different controls, but I think they should have consistency on basic behaviors.

@ghost1372
Copy link
Contributor

Your sample project link is broken...

Sorry about that. Fixed the link.

I think this is happening because the treeview control is not loaded yet!
Therefore, instead of setting the selectedItem in the constructor, set it in the page loaded event

The SelectedItem is not set in the constructor, it's just bound to a property in the view model. ListView, GridView and NavigationView are under the same state but only the TreeView doesn't select the bound item.

Loaded event working fine for me!

private void Page_Loaded(object sender, Microsoft.UI.Xaml.RoutedEventArgs e)
{
    ViewModel.init();
}

public void init()
{
    SelectedItem = Items[1];
}

@ghost1372
Copy link
Contributor

Image

@AndrewKeepCoding
Copy link
Contributor Author

Thanks for confirming it but I guess the TreeView control should have the same basic behaviors as the other controls.

@ghost1372
Copy link
Contributor

Thanks for confirming it but I guess the TreeView control should have the same basic behaviors as the other controls.

Therefore you should create a new issue and mentation that ctor is not working in treeview

@AndrewKeepCoding AndrewKeepCoding changed the title TreeView does not respect initial SelectedItem when loaded TreeView does not respect initial SelectedItem when initialized Oct 30, 2024
@karkarl karkarl added area-TreeView and removed needs-triage Issue needs to be triaged by the area owners labels Oct 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-TreeView bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants