[RS3:1709] [UWP] [XAML]如何在DataTemplate中使用NavigationViewItem? [英] [RS3:1709][UWP] [XAML] How to use NavigationViewItem in DataTemplate?

查看:218
本文介绍了[RS3:1709] [UWP] [XAML]如何在DataTemplate中使用NavigationViewItem?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想动态创建在NavigationView中填充MenuItems。所以我在MenuItemTemplate中使用了NavigationViewItem,但它不起作用(正如我所料)。我不想将StackPanel与SymbolIcon和TextBlock一起使用。

I want to create dynamically populate MenuItems in NavigationView. So I use NavigationViewItem inside MenuItemTemplate but it doesn't work (As I expected). I don't want to use StackPanel with SymbolIcon and TextBlock.

你可以看到输出图像在这里

这是我的代码。

<NavigationView OpenPaneLength="280" CompactModeThresholdWidth="560" ExpandedModeThresholdWidth="560" MenuItemsSource="{x:Bind MenuItems}" IsSettingsVisible="True" HeaderTemplate="{StaticResource NavigationHeaderDataTemplate}" ItemInvoked="NavigationView_ItemInvoked">

    <NavigationView.MenuItemTemplate>
        <DataTemplate x:DataType="local:ShellNavigationItem">
            <NavigationViewItem Icon="{x:Bind Symbol, Mode=OneWay}" Content="{x:Bind Label, Mode=OneWay}"/>
        </DataTemplate>
    </NavigationView.MenuItemTemplate>

    <Frame x:Name="ShellFrame"/>
</NavigationView>


那么我如何使用NavigationViewItem动态填充MenuItem?




So How can I use NavigationViewItem for dynamically populate MenuItems?


推荐答案

嗨Vijay Nirmal

>>所以我在MenuItemTemplate中使用了NavigationViewItem但它不起作用(正如我所料)。

MenuItemTemplate
用于获取或设置DataTemplate以显示每个菜单项
。将NavigationViewItem放入NavigationViewItem模板不是一个好习惯。

MenuItemTemplate is used to get or set the DataTemplate for displaying each menu item. It is not a good practice to put the NavigationViewItem into the NavigationViewItem template.

通常,我们更有可能在使用DataTemplate时重新定义样式。在您的场景中,它将使用带有SymbolIcon和TextBlock的StackPanel
为什么您不想使用这种方式?

In general, we are more likely to redefine the style when using the DataTemplate. In your scenario, it would be using StackPanel with SymbolIcon and TextBlock. Why you don’t want to use this way?

如果你坚持在模板中使用NavigationViewItem,你可以设置" IsHitTestVisible"为假。并且ItemInvoked
事件将被正确触发。

<NavigationViewItem IsHitTestVisible="False" Icon="{x:Bind Symbol, Mode=OneWay}"  Content="{x:Bind Label, Mode=OneWay}"  />


祝你好运,

Roy


这篇关于[RS3:1709] [UWP] [XAML]如何在DataTemplate中使用NavigationViewItem?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆