question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

My NavigationView Items background color doesn't revert to its unselected color when I change selections. I have been trying all kinds of styles and settings and can't seem to get this working correctly.

See original GitHub issue

3b846060e2ba92e999d5fcd873fa67cf

Current XAML:

    <Control.Resources>
        <vmc:NullableIntToIntConverter  x:Key="NullableIntToIntConverter"/>

        <SolidColorBrush x:Key="NavigationViewTopPaneBackground" Color="White"/>
        <SolidColorBrush x:Key="TopNavigationViewItemForegroundPointerOver" Color="#78a2c7"/>
        <SolidColorBrush x:Key="TopNavigationViewItemForegroundPressed" Color="Black"/>
        <SolidColorBrush x:Key="TopNavigationViewItemBackgroundPointerOver" Color="#a3c0d9"/>
        <SolidColorBrush x:Key="TopNavigationViewItemBackgroundPressed" Color="#78a2c7"/>
        <SolidColorBrush x:Key="TopNavigationViewItemBackgroundSelected" Color="White"/>

        <SolidColorBrush x:Key="NavigationViewItemForeground" Color="White"/>
        <SolidColorBrush x:Key="NavigationViewItemBackground" Color="#477DAC"/>
        <SolidColorBrush x:Key="NavigationViewItemBorderBrush" Color="#6696c0"/>
        <SolidColorBrush x:Key="NavigationViewSelectionIndicatorForeground" Color="#477DAC"/>

        <DataTemplate x:Key="MasterNavItemTemplate">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"/>
                </Grid.RowDefinitions>
                <TextBlock Text="{Binding HeaderTitle, Mode=OneWay}" 
                        Style="{StaticResource NavigationViewItemHeaderTextStyle}"
                        FontWeight="Normal" 
                        FontSize="20"/>
            </Grid>
        </DataTemplate>

        <DataTemplate x:Key="DetailContentTemplate">
            <ScrollViewer VerticalScrollMode="Auto" BorderBrush="#477DAC" BorderThickness="1"  >
                <ContentPresenter Content="{Binding Content}" Padding="0,0,8,0" />
            </ScrollViewer>
        </DataTemplate>

    </Control.Resources>

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <muxc:NavigationView 
            x:Name="nvSubMenu" 
            IsBackButtonVisible="Collapsed"
            IsSettingsVisible="False"
            IsEnabled="True"
            HorizontalAlignment="Left"
            PaneDisplayMode="Top"
            IsPaneToggleButtonVisible="False"
            AlwaysShowHeader="False"
            MenuItemsSource="{Binding SubLevelMenuList}"
            MenuItemTemplate="{StaticResource MasterNavItemTemplate}">
        </muxc:NavigationView>

        <ContentPresenter
            x:Name="DetailContentPresenter"
            Grid.Row="1"
            HorizontalAlignment="Left" VerticalAlignment="Top"
            Content="{x:Bind nvSubMenu.SelectedItem, Mode=OneWay, TargetNullValue=1}"
            ContentTemplate="{StaticResource DetailContentTemplate}">
        </ContentPresenter>

    </Grid>

Visual Studio 2019 Enterprise Version 16.6.5 Target version: Windows 10, version 1903 (10.0; Build 18362) Min version: Windows 10, version 1809 (10.0; Build 17763)

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:13 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
ranjeshjcommented, Aug 21, 2020

I don’t think it is an issue with your code. It does look like the bug that was fixed. The default colors have the same background for normal and pressed, so I’m guessing it repros there as well but just not visible. If you can update and ship on the pre-release that would be a better option. But If you absolutely cannot update, then this is a super hacky workaround to update the presenter visual state by hand.

 private void NvMenu_SelectionChanged(Microsoft.UI.Xaml.Controls.NavigationView sender, Microsoft.UI.Xaml.Controls.NavigationViewSelectionChangedEventArgs args)
        {
            foreach(var item in Headers)
            {
                var navItem = nvMenu.ContainerFromMenuItem(item) as Microsoft.UI.Xaml.Controls.NavigationViewItem;
                if(!navItem.IsSelected)
                {
                    var grid = VisualTreeHelper.GetChild(navItem, 0) as Grid;
                    if (grid != null)
                    {
                        var presenter = VisualTreeHelper.GetChild(grid, 0) as Microsoft.UI.Xaml.Controls.Primitives.NavigationViewItemPresenter;
                        if (presenter != null)
                        {
                            VisualStateManager.GoToState(presenter, "Normal", true);
                        }
                    }
                }
            }
        }
0reactions
Kcomidcommented, Sep 30, 2020

It appears to be working fine. My original concern was that it was in a prerelease versus an official release. Closing it is fine. If something changes I will let you know. Thanks.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Change the color of a checked menu item in a navigation ...
Final step would be to change your NavigationView ... Step 4: To change icon color: set the selector to all the items with...
Read more >
Navigation component no longer shows option for ...
I went to update a style on my navigation component but the option ... shows option for background color for selected menu item...
Read more >
Change background colour of mobile nav menu
Change background colour of mobile nav menu · 1) Select Header · 2) Set the view to mobile on the bottom settings part...
Read more >
Bottom navigation - Material Design
Bottom navigation bars allow movement between primary destinations in an app.
Read more >
Swiftui list disable selection. items, id: \. Also there's an AD
The SwiftUI List view has many styles to choose from. ... But the problem is that it doesn't change the text color to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found