how can we highlight navigation view item when selected with button in winui3

shreshta valluru 86 Reputation points
2022-05-11T04:38:42.493+00:00

when navigation view item is selected it should higlight with customised color

Windows App SDK
Windows App SDK
A set of Microsoft open-source libraries, frameworks, components, and tools to be used in apps to access Windows platform functionality on many versions of Windows. Previously known as Project Reunion.
878 questions
{count} votes

Accepted answer
  1. Roy Li - MSFT 34,286 Reputation points Microsoft External Staff
    2022-05-11T07:32:16.887+00:00

    Hello,

    Welcome to Microsoft Q&A!

    You could try to override the corresponding theme resource for the item selected status.

    Like this:

            <NavigationView x:Name="NavigationViewControl" >  
                <NavigationView.Resources>  
                    <SolidColorBrush x:Key="NavigationViewItemBackgroundSelected" Color="Yellow"/>  
                </NavigationView.Resources>  
                <NavigationView.MenuItems>  
                    <NavigationViewItem Content="A" x:Name="A" />  
                    <NavigationViewItem Content="B" x:Name="B" />  
                    <NavigationViewItem Content="C" x:Name="C" />  
                </NavigationView.MenuItems>  
            </NavigationView>  
    

    Thank you.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


1 additional answer

Sort by: Most helpful
  1. Cohn, Robert 21 Reputation points
    2022-08-25T03:52:45.137+00:00

    Hello @Roy Li - MSFT -

    Your answer was perfect but for a different reason! It seems that WinUI3 NavigationView leaves highlighting tracks, even when the NavigationViewItem is no longer selected. So if there are three items and the user clicks on each in succession, the selection bar moves properly to one item at a time, but the highlighting stays. You can end up with all items highlighted.

    But with your answer, I was able to set NavigationViewItemBackgroundSelected to Transparent and the problem is no longer visible. Note that items still get highlighted during cursor hover, but that's OK.

    Yes - this is a hack solution, but at least it solves an immediate problem. If you know of why multiple items are highlighted, I would be delighted to find out. Most likely there is something like a multiple selection property, but I can't find it. Or this is yet another WinUI3 NavigationView bug.

    Anyway, many thanks for your help.

    Best regards.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.