How to change the Content and IsSelected properties of a NavigationViewItem in WinUI 3 by locating it using its x:name or tag?

huangapple go评论86阅读模式
英文:

How to change the Content and IsSelected properties of a NavigationViewItem in WinUI 3 by locating it using its x:name or tag?

问题

  1. 我创建了一些 `NavigationViewItem` 并设置了它们的 `x:Name` `Tag`,如下所示:
  2. ```xaml
  3. <NavigationView x:Name="nvSample"
  4. SelectionChanged="nvSample_SelectionChanged"
  5. IsBackButtonVisible="Collapsed"
  6. IsSettingsVisible="False">
  7. <NavigationView.MenuItems>
  8. <NavigationViewItem x:Name="NavigationViewItem_HomePage" Icon="Home" Content="Home" Tag="HomePage" IsSelected="True" />
  9. <NavigationViewItem x:Name="NavigationViewItem_SimpleSortPage" Icon="Sort" Content="Sort" Tag="SimpleSortPage" />
  10. <...........>
  11. </NavigationView.MenuItems>
  12. </NavigationView>

我想要通过它们的 x:NameTag 来查找 NavigationViewItem 对象,以便我可以在代码中更改 ContentIsSelected 属性。

我发现 Type.GetType 可以通过 x:Name 来获取页面,例如:

  1. Type pageSelected = Type.GetType("NumberSort.Pages.SimpleSort");
  2. RootPageFrame.Navigate(pageSelected,this);

所以我尝试使用它来获取 NavigationViewItem 对象,例如:

  1. var NavigationItem = (NavigationViewItem)Type.GetType("NavigationViewItem_HomePage");

然而,这并不起作用。

  1. <details>
  2. <summary>英文:</summary>
  3. I create some `NavigationViewItem` and set its `x:Name` and `Tag`, like:
  4. ```xaml
  5. &lt;NavigationView x:Name=&quot;nvSample&quot;
  6. SelectionChanged=&quot;nvSample_SelectionChanged&quot;
  7. IsBackButtonVisible=&quot;Collapsed&quot;
  8. IsSettingsVisible=&quot;False&quot;&gt;
  9. &lt;NavigationView.MenuItems&gt;
  10. &lt;NavigationViewItem x:Name=&quot;NavigationViewItem_HomePage&quot; Icon=&quot;Home&quot; Content=&quot;Home&quot; Tag=&quot;HomePage&quot; IsSelected=&quot;True&quot; /&gt;
  11. &lt;NavigationViewItem x:Name=&quot;NavigationViewItem_SimpleSortPage&quot; Icon=&quot;Sort&quot; Content=&quot;Sort&quot; Tag=&quot;SimpleSortPage&quot; /&gt;
  12. &lt;...........&gt;
  13. &lt;/NavigationView.MenuItems&gt;
  14. &lt;/NavigationView&gt;

I want to find the NavigationViewItem object by its x:Name or Tag so that I can change Content and IsSelected property by code.

I find Type.GetType may get a page by x:Name, like:

  1. Type pageSelected = Type.GetType(&quot;NumberSort.Pages.SimpleSort&quot;);
  2. RootPageFrame.Navigate(pageSelected,this);

So I tried to use it to get the NavigationViewItem object, like:

  1. var NavigationItem = (NavigationViewItem)Type.GetType(&quot;NavigationViewItem_HomePage&quot;);

However, its doesn't work.

答案1

得分: 0

  1. 您可以创建像这样的扩展方法:
  2. **NavigationViewExtensions.cs**
  3. ```cs
  4. public static class NavigationViewExtensions
  5. {
  6. // 从 NavigationViewItem 获取所有 NavigationViewItem 。
  7. public static IEnumerable&lt;NavigationViewItem&gt; GetAllNavigationViewItems(this NavigationViewItem navigationViewItem)
  8. {
  9. if (navigationViewItem.MenuItems is null)
  10. {
  11. yield break;
  12. }
  13. foreach (var menuItem in navigationViewItem.MenuItems.OfType&lt;NavigationViewItem&gt;())
  14. {
  15. yield return menuItem;
  16. if (menuItem.MenuItems is null)
  17. {
  18. continue;
  19. }
  20. foreach (NavigationViewItem subMenuItem in menuItem.MenuItems.OfType&lt;NavigationViewItem&gt;())
  21. {
  22. yield return subMenuItem;
  23. }
  24. }
  25. }
  26. // 从 NavigationView 获取所有 NavigationViewItem 。
  27. public static IEnumerable&lt;NavigationViewItem&gt; GetAllNavigationViewItems(this NavigationView navigationView)
  28. {
  29. foreach (NavigationViewItem menuItem in navigationView.MenuItems.OfType&lt;NavigationViewItem&gt;())
  30. {
  31. yield return menuItem;
  32. if (menuItem.MenuItems is null)
  33. {
  34. continue;
  35. }
  36. foreach (NavigationViewItem subMenuItem in menuItem.GetAllNavigationViewItems())
  37. {
  38. yield return subMenuItem;
  39. }
  40. }
  41. }
  42. }

并像这样使用它:

  1. if (this.nvSample
  2. .GetAllNavigationViewItems()
  3. .FirstOrDefault(x =&gt; x.Name is &quot;NavigationViewItem_HomePage&quot;) is NavigationViewItem navigationViewItem)
  4. {
  5. // 在这里做一些事情。
  6. }
英文:

You can create extension methods like these:

NavigationViewExtensions.cs

  1. public static class NavigationViewExtensions
  2. {
  3. // Get all NavigationViewItems from a NavigationViewItem.
  4. public static IEnumerable&lt;NavigationViewItem&gt; GetAllNavigationViewItems(this NavigationViewItem navigationViewItem)
  5. {
  6. if (navigationViewItem.MenuItems is null)
  7. {
  8. yield break;
  9. }
  10. foreach (var menuItem in navigationViewItem.MenuItems.OfType&lt;NavigationViewItem&gt;())
  11. {
  12. yield return menuItem;
  13. if (menuItem.MenuItems is null)
  14. {
  15. continue;
  16. }
  17. foreach (NavigationViewItem subMenuItem in menuItem.MenuItems.OfType&lt;NavigationViewItem&gt;())
  18. {
  19. yield return subMenuItem;
  20. }
  21. }
  22. }
  23. // Get all NavigationViewItems from a NavigationView.
  24. public static IEnumerable&lt;NavigationViewItem&gt; GetAllNavigationViewItems(this NavigationView navigationView)
  25. {
  26. foreach (NavigationViewItem menuItem in navigationView.MenuItems.OfType&lt;NavigationViewItem&gt;())
  27. {
  28. yield return menuItem;
  29. if (menuItem.MenuItems is null)
  30. {
  31. continue;
  32. }
  33. foreach (NavigationViewItem subMenuItem in menuItem.GetAllNavigationViewItems())
  34. {
  35. yield return subMenuItem;
  36. }
  37. }
  38. }
  39. }

and use it like this:

  1. if (this.nvSample
  2. .GetAllNavigationViewItems()
  3. .FirstOrDefault(x =&gt; x.Name is &quot;NavigationViewItem_HomePage&quot;) is NavigationViewItem navigationViewItem)
  4. {
  5. // Do something here.
  6. }

答案2

得分: 0

我找到了一个简单的方法来做到这一点:

  1. public static void SwitchPage(string pageName, NavigationView navigationView)
  2. {
  3. var navigationViewItems = navigationView.MenuItems.OfType<NavigationViewItem>().ToList();
  4. if (navigationViewItems != null)
  5. {
  6. foreach (var item in navigationViewItems)
  7. {
  8. if (item.Tag.Equals(pageName))
  9. {
  10. item.IsSelected = true; break;
  11. }
  12. }
  13. }
  14. else { throw new ArgumentNullException(); }
  15. }

在这个示例中,它将 IsSelected 属性设置为 true,当 pageNameNavigationViewItemTag 属性相等时。

英文:

I found a easy way to do that:

  1. public static void SwitchPage (string pageName,NavigationView navigationView)
  2. {
  3. var navigationViewItems = navigationView.MenuItems.OfType&lt;NavigationViewItem&gt;().ToList();
  4. if (navigationViewItems != null) {
  5. foreach (var item in navigationViewItems)
  6. {
  7. if( item.Tag.Equals(pageName))
  8. {
  9. item.IsSelected = true; break;
  10. }
  11. }
  12. }
  13. else {throw new ArgumentNullException()};
  14. }

In this example, it sets IsSelected property of NavigationViewItem as true, which pageName is equal with
Tag property of NavigationViewItem.

huangapple
  • 本文由 发表于 2023年5月25日 09:23:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76328316.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定