简体   繁体   中英

NavigationView - When selected settings item there is no way to unselect it from code

I've found some strange behavior. If SelectedItem is SettingsItem then cannot deselect it from code

XAML:

<NavigationView Name="nv">
    <NavigationView.MenuItems>
        <NavigationViewItem Content="dsadas" />
        <NavigationViewItem Content="dsadas" />
        <NavigationViewItem Content="dsadas" />
        <NavigationViewItem Content="dsadas" />
    </NavigationView.MenuItems>
    <Button Click="Button_Click" Content="de select" />
</NavigationView>

CS:

private void Button_Click(object sender, RoutedEventArgs e)
{
    nv.SelectedItem = null;
}

And that is all. I don't understand why code doesn't work for Settings Item and how to deal with this problem.

Extending @Martin-Zikmund answer. Code handling now situation if ItemsSource is set.

public static class NavigationViewExtension
{
    public static void ClearSelection(this NavigationView navigationView)
    {
        var temporaryItem = new NavigationViewItem();

        if (navigationView.MenuItemsSource != null)
        {
            var existingList = ((IList)(navigationView.MenuItemsSource));
            existingList.Add(temporaryItem);
            navigationView.SelectedItem = temporaryItem;
            navigationView.SelectedItem = null;
            existingList.Remove(temporaryItem);
        }
        else
        {
            navigationView.MenuItems.Add(temporaryItem);
            navigationView.SelectedItem = temporaryItem;
            navigationView.SelectedItem = null;
            navigationView.MenuItems.Remove(temporaryItem);
        }
    }
}

This works for me completely

This is very interesting bug in the NavigationView , I have created a GitHub repro and reported it via Feedback Hub .

As a workaround I have created the following "hack" extension method:

public static class NavigationViewExtensions
{
    public static void ClearSelection(this NavigationView navigationView)
    {
        var temporaryItem = new NavigationViewItem();
        navigationView.MenuItems.Add(temporaryItem);
        navigationView.SelectedItem = temporaryItem;
        navigationView.SelectedItem = null;
        navigationView.MenuItems.Remove(temporaryItem);
    }
}

How does it work? The code temporarily adds a new NavigationViewItem to the control and selects it (thus deselecting the Settings item). Then it clears the selection and a removes the temporary item right away. Because the view is updated on the UI thread, you will never be able to notice the new item being added and removed again.

I found another way to solve it.

You need to add hidden item to NavigationView:

<NavigationView Name="nv">
  <NavigationView.MenuItems>
    <NavigationViewItem Content="dsadas" />
    <NavigationViewItem Content="dsadas" />
    <NavigationViewItem Content="dsadas" />
    <NavigationViewItem Content="dsadas" />
    <NavigationViewItem x:Name="HiddenItem" Visibility="Collapsed" />
  </NavigationView.MenuItems>
<Button Click="Button_Click" Content="de select" />

And then you can clear selection:

nv.SelectedItem = HiddenItem;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM