英文:
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:Name
或 Tag
来查找 NavigationViewItem
对象,以便我可以在代码中更改 Content
和 IsSelected
属性。
我发现 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
<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>
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("NumberSort.Pages.SimpleSort");
RootPageFrame.Navigate(pageSelected,this);
So I tried to use it to get the NavigationViewItem
object, like:
var NavigationItem = (NavigationViewItem)Type.GetType("NavigationViewItem_HomePage");
However, its doesn't work.
答案1
得分: 0
您可以创建像这样的扩展方法:
**NavigationViewExtensions.cs**
```cs
public static class NavigationViewExtensions
{
// 从 NavigationViewItem 获取所有 NavigationViewItem 。
public static IEnumerable<NavigationViewItem> GetAllNavigationViewItems(this NavigationViewItem navigationViewItem)
{
if (navigationViewItem.MenuItems is null)
{
yield break;
}
foreach (var menuItem in navigationViewItem.MenuItems.OfType<NavigationViewItem>())
{
yield return menuItem;
if (menuItem.MenuItems is null)
{
continue;
}
foreach (NavigationViewItem subMenuItem in menuItem.MenuItems.OfType<NavigationViewItem>())
{
yield return subMenuItem;
}
}
}
// 从 NavigationView 获取所有 NavigationViewItem 。
public static IEnumerable<NavigationViewItem> GetAllNavigationViewItems(this NavigationView navigationView)
{
foreach (NavigationViewItem menuItem in navigationView.MenuItems.OfType<NavigationViewItem>())
{
yield return menuItem;
if (menuItem.MenuItems is null)
{
continue;
}
foreach (NavigationViewItem subMenuItem in menuItem.GetAllNavigationViewItems())
{
yield return subMenuItem;
}
}
}
}
并像这样使用它:
if (this.nvSample
.GetAllNavigationViewItems()
.FirstOrDefault(x => x.Name is "NavigationViewItem_HomePage") 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<NavigationViewItem> GetAllNavigationViewItems(this NavigationViewItem navigationViewItem)
{
if (navigationViewItem.MenuItems is null)
{
yield break;
}
foreach (var menuItem in navigationViewItem.MenuItems.OfType<NavigationViewItem>())
{
yield return menuItem;
if (menuItem.MenuItems is null)
{
continue;
}
foreach (NavigationViewItem subMenuItem in menuItem.MenuItems.OfType<NavigationViewItem>())
{
yield return subMenuItem;
}
}
}
// Get all NavigationViewItems from a NavigationView.
public static IEnumerable<NavigationViewItem> GetAllNavigationViewItems(this NavigationView navigationView)
{
foreach (NavigationViewItem menuItem in navigationView.MenuItems.OfType<NavigationViewItem>())
{
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 => x.Name is "NavigationViewItem_HomePage") 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
,当 pageName
与 NavigationViewItem
的 Tag
属性相等时。
英文:
I found a easy way to do that:
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()};
}
In this example, it sets IsSelected
property of NavigationViewItem
as true
, which pageName
is equal with
Tag
property of NavigationViewItem
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论