microsoft / microsoft-ui-xaml

WinUI: a modern UI framework with a rich set of controls and styles to build dynamic and high-performing Windows applications.
MIT License
6.38k stars 683 forks source link

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. #3049

Closed Kcomid closed 4 years ago

Kcomid commented 4 years ago

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)

StephenLPeters commented 4 years ago

@ranjeshj and @ojhad @llongley seems similar to the issue you saw a couple weeks ago. @Kcomid I don't think you are doing anything wrong.. This is probably a bug in the Navigation View's VSM

ranjeshj commented 4 years ago

@Kcomid which version of WinUI are you using? There have been some recent fixes specifically this one. Could you try the latest pre-release ?

Kcomid commented 4 years ago

We are using Microsoft.UI.Xaml Version 2.4.2. I will try the pre-release and let you know. Thanks.

Kcomid commented 4 years ago

I have tried the new prerelease and things seem to be working better. Do you know when this will be released? I can’t use the prerelease in my project. Thank you.

ranjeshj commented 4 years ago

@Kcomid I don't have a timeline yet for the next stable release. Most of the team is focused on WinUI3 at the moment.

Going-Gone commented 4 years ago

@ranjeshj Is there any way around this for the time being in the 2.4 release?

ranjeshj commented 4 years ago

@Going-Gone I just switched Xaml Controls Gallery to 2.4.3 and I can't reproduce the issue. If you can share a repro app, we can try to see what is different and if we can find a workaround. Just out of curiosity, Is there a reason you cannot use the pre-release ? Shipping production apps on pre-release WinUI2 versions is possible.

Going-Gone commented 4 years ago

@ranjeshj Ah Will try 2.4.3. Didn't see that nuget version i guess. Was using 2.4.2 Thanks.

Going-Gone commented 4 years ago

@ranjeshj Here is the repo. https://github.com/Going-Gone/NavViewWinUI2.4.2 The latest pre release works great. We may have to end up using it, but wanted to see if there was any work around first.

Issue still seems to be there in 2.4.3. Maybe I'm doing something wrong.

Simple expectation is The goal is to have the menu blue When selected white. If not selected blue.

ranjeshj commented 4 years ago

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);
                        }
                    }
                }
            }
        }
marcelwgn commented 4 years ago

It seems that this issue doesn't repro in the latest prerelease bits. @StephenLPeters @ranjeshj Is there anything to investigate here or can this be closed?

StephenLPeters commented 4 years ago

I don't think so, closing the issue, @Kcomid if there is still something wrong post back here and I'll reopen.

Kcomid commented 4 years ago

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.