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

The active triggers in NavigationView are not displayed correctly #1059

Open
BoyFaceGirl opened this issue Apr 18, 2024 · 1 comment
Open
Labels
bug Something isn't working

Comments

@BoyFaceGirl
Copy link

BoyFaceGirl commented Apr 18, 2024

Describe the bug

 I need to use it in conjunction with the Prism framework and do not require

the TargetPageType attribute. When deleting the TargetPageType attribute or leaving it null,
there will be an error in displaying the selected style

PixPin_2024-04-18_08-48-08

To Reproduce

When deleting the TargetPageType attribute or leaving it null, there will be an error in displaying the selected style。

<ui:NavigationView
    x:Name="RootNavigation"
    Grid.Row="1"
    Margin="0"
    Padding="0"
    IsBackButtonVisible="Collapsed">
    <ui:NavigationView.AutoSuggestBox>
        <ui:AutoSuggestBox x:Name="AutoSuggestBox" PlaceholderText="Search">
            <ui:AutoSuggestBox.Icon>
                <ui:IconSourceElement>
                    <ui:SymbolIconSource Symbol="Search24" />
                </ui:IconSourceElement>
            </ui:AutoSuggestBox.Icon>
        </ui:AutoSuggestBox>
    </ui:NavigationView.AutoSuggestBox>
    <ui:NavigationView.Header>
        <ui:BreadcrumbBar
            Margin="42,32,0,0"
            FontSize="28"
            FontWeight="DemiBold" />
    </ui:NavigationView.Header>
    <ui:NavigationView.MenuItems>
        <ui:NavigationViewItem Content="Dashboard" NavigationCacheMode="Enabled">
            <ui:NavigationViewItem.Icon>
                <ui:SymbolIcon Symbol="Home24" />
            </ui:NavigationViewItem.Icon>
        </ui:NavigationViewItem>
        <ui:NavigationViewItem Content="Data" NavigationCacheMode="Enabled">
            <ui:NavigationViewItem.Icon>
                <ui:SymbolIcon Symbol="DataHistogram24" />
            </ui:NavigationViewItem.Icon>
        </ui:NavigationViewItem>
    </ui:NavigationView.MenuItems>
</ui:NavigationView>

Expected behavior

This issue occurs when the NavigationViewItem does not have the TargetPageType property set. Inference and IsActive trigger xaml style incorrect

Screenshots

error:
PixPin_2024-04-18_08-48-08

success:

PixPin_2024-04-18_08-51-55

OS version

win11

.NET version

.net8

WPF-UI NuGet version

3.0.4

Additional context

No response

@BoyFaceGirl BoyFaceGirl added the bug Something isn't working label Apr 18, 2024
@BoyFaceGirl
Copy link
Author

BoyFaceGirl commented Apr 18, 2024

I have found a feasible solution by adding the following recursive method to NavigationViewItem. cs:

    private void ClearActiveStatus(IList menuitems, NavigationView navigationView)
    {
        foreach (var item in menuitems)
        {
            var nvaitem = item as NavigationViewItem;

            if (nvaitem != null)
            {
                if (nvaitem.IsActive)
                {
                    nvaitem.Deactivate(navigationView);
                }

                if (nvaitem.HasMenuItems)
                {
                    ClearActiveStatus(nvaitem.MenuItems, navigationView);
                }
            }
        }
    }

Then call within Onclick:

  var navigation = NavigationView.GetNavigationParent(this);
  if (navigation is not null)
  {
      IList menuitems = navigation.MenuItems;

      ClearActiveStatus(menuitems, navigation);

      Activate(navigation);
  }

The complete modification code is:

protected override void OnClick()
 {
     if (NavigationView.GetNavigationParent(this) is not { } navigationView)
     {
         return;
     }

     var navigation = NavigationView.GetNavigationParent(this);
     if (navigation is not null)
     {
         IList menuitems = navigation.MenuItems;
         ClearActiveStatus(menuitems, navigation);
         Activate(navigation);
     }

     if (HasMenuItems && navigationView.IsPaneOpen)
     {
         SetCurrentValue(IsExpandedProperty, !IsExpanded);
     }

     if (TargetPageType is not null)
     {
         navigationView.OnNavigationViewItemClick(this);
     }

     base.OnClick();
 }

 private void ClearActiveStatus(IList menuitems, NavigationView navigationView)
 {
     foreach (var item in menuitems)
     {
         var navitem = item as NavigationViewItem;

         if (navitem != null)
         {
             if (navitem.IsActive)
             {
                 navitem.Deactivate(navigationView);
             }

             if (navitem.HasMenuItems)
             {
                 ClearActiveStatus(navitem.MenuItems, navigationView);
             }
         }
     }
 }

🚀Someone can help review this code for any issues, and it would be even better if they could also assist in submitting the merge.😀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant