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评论75阅读模式
英文:

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

问题

我创建了一些 `NavigationViewItem` 并设置了它们的 `x:Name` 和 `Tag`,如下所示:

```xaml
<NavigationView x:Name="nvSample"
                SelectionChanged="nvSample_SelectionChanged"
                IsBackButtonVisible="Collapsed"
                IsSettingsVisible="False">

        <NavigationView.MenuItems>

            <NavigationViewItem x:Name="NavigationViewItem_HomePage" Icon="Home" Content="Home" Tag="HomePage" IsSelected="True" />
            <NavigationViewItem x:Name="NavigationViewItem_SimpleSortPage" Icon="Sort" Content="Sort" Tag="SimpleSortPage" />

            <...........>

        </NavigationView.MenuItems>

</NavigationView>

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

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

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

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

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

然而,这并不起作用。


<details>
<summary>英文:</summary>

I create some `NavigationViewItem` and set its `x:Name` and `Tag`, like:

```xaml
&lt;NavigationView x:Name=&quot;nvSample&quot;
                SelectionChanged=&quot;nvSample_SelectionChanged&quot;
                IsBackButtonVisible=&quot;Collapsed&quot;
                IsSettingsVisible=&quot;False&quot;&gt;

        &lt;NavigationView.MenuItems&gt;

            &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;
            &lt;NavigationViewItem x:Name=&quot;NavigationViewItem_SimpleSortPage&quot; Icon=&quot;Sort&quot; Content=&quot;Sort&quot; Tag=&quot;SimpleSortPage&quot; /&gt;

            &lt;...........&gt;

        &lt;/NavigationView.MenuItems&gt;

&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:

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

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

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

However, its doesn't work.

答案1

得分: 0

您可以创建像这样的扩展方法:

**NavigationViewExtensions.cs**
```cs
public static class NavigationViewExtensions
{
    // 从 NavigationViewItem 获取所有 NavigationViewItem 。
    public static IEnumerable&lt;NavigationViewItem&gt; GetAllNavigationViewItems(this NavigationViewItem navigationViewItem)
    {
        if (navigationViewItem.MenuItems is null)
        {
            yield break;
        }

        foreach (var menuItem in navigationViewItem.MenuItems.OfType&lt;NavigationViewItem&gt;())
        {
            yield return menuItem;

            if (menuItem.MenuItems is null)
            {
                continue;
            }

            foreach (NavigationViewItem subMenuItem in menuItem.MenuItems.OfType&lt;NavigationViewItem&gt;())
            {
                yield return subMenuItem;
            }
        }
    }

    // 从 NavigationView 获取所有 NavigationViewItem 。
    public static IEnumerable&lt;NavigationViewItem&gt; GetAllNavigationViewItems(this NavigationView navigationView)
    {
        foreach (NavigationViewItem menuItem in navigationView.MenuItems.OfType&lt;NavigationViewItem&gt;())
        {
            yield return menuItem;

            if (menuItem.MenuItems is null)
            {
                continue;
            }

            foreach (NavigationViewItem subMenuItem in menuItem.GetAllNavigationViewItems())
            {
                yield return subMenuItem;
            }
        }
    }
}

并像这样使用它:

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

You can create extension methods like these:

NavigationViewExtensions.cs

public static class NavigationViewExtensions
{
    // Get all NavigationViewItems from a NavigationViewItem.
    public static IEnumerable&lt;NavigationViewItem&gt; GetAllNavigationViewItems(this NavigationViewItem navigationViewItem)
    {
        if (navigationViewItem.MenuItems is null)
        {
            yield break;
        }

        foreach (var menuItem in navigationViewItem.MenuItems.OfType&lt;NavigationViewItem&gt;())
        {
            yield return menuItem;

            if (menuItem.MenuItems is null)
            {
                continue;
            }

            foreach (NavigationViewItem subMenuItem in menuItem.MenuItems.OfType&lt;NavigationViewItem&gt;())
            {
                yield return subMenuItem;
            }
        }
    }

    // Get all NavigationViewItems from a NavigationView.
    public static IEnumerable&lt;NavigationViewItem&gt; GetAllNavigationViewItems(this NavigationView navigationView)
    {
        foreach (NavigationViewItem menuItem in navigationView.MenuItems.OfType&lt;NavigationViewItem&gt;())
        {
            yield return menuItem;

            if (menuItem.MenuItems is null)
            {
                continue;
            }

            foreach (NavigationViewItem subMenuItem in menuItem.GetAllNavigationViewItems())
            {
                yield return subMenuItem;
            }
        }
    }
}

and use it like this:

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

答案2

得分: 0

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

public static void SwitchPage(string pageName, NavigationView navigationView)
{

    var navigationViewItems = navigationView.MenuItems.OfType<NavigationViewItem>().ToList();

    if (navigationViewItems != null)
    {
        foreach (var item in navigationViewItems)
        {
            if (item.Tag.Equals(pageName))
            {
                item.IsSelected = true; break;
            }
        }
    }
    else { throw new ArgumentNullException(); }
}

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

英文:

I found a easy way to do that:

public static void SwitchPage (string pageName,NavigationView navigationView)
{

    var navigationViewItems = navigationView.MenuItems.OfType&lt;NavigationViewItem&gt;().ToList();

    if (navigationViewItems != null) {
        foreach (var item in navigationViewItems)
        {
            if( item.Tag.Equals(pageName))
            {
                item.IsSelected = true; break;
            }
        }
    }
    else {throw new ArgumentNullException()};
}

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:

确定