我正在尝试更改 NavigationView
的颜色. documentation给出了如何更改背景的一个很好的示例,但我找不到如何更改菜单项的字体颜色。
我还可以使用 <SolidColorBrush x:Key="SystemControlForegroundAccentBrush" Color="#FFFFFFFF" />
更改选择指示器的颜色
最佳答案
NavigationView 是自动使用 Reveal highlight 的众多控件之一,它也是新的 Fluent Design System 组件之一,其目的是为应用程序添加光。
@A.如果我们处理的控件不像 NavigationViewItem 那样共享那么多状态,那么 Milto 解决方案将起作用,但是 NavigationMenuItem 具有:
- 指针指向;
- 按下
- 已选中;
- PointerOverSelected;
- 按下选择;
不考虑它的禁用状态。 简单地将前景属性强制为您想要的颜色,将提供以下内容:
实现您的意图的最简单解决方案是覆盖 NavigationViewItem 模板中的资源,您实际上可以在 generic.xaml 中 check out 这些资源。你可以这样做,像这样:
<Grid.Resources>
<SolidColorBrush x:Key="NavigationViewItemForegroundPointerOver" Color="Red"/>
<SolidColorBrush x:Key="NavigationViewItemForegroundSelected" Color="Red"/>
<SolidColorBrush x:Key="NavigationViewItemForegroundSelectedPointerOver" Color="Red"/>
<SolidColorBrush x:Key="NavigationViewItemForegroundPressed" Color="Red"/>
<SolidColorBrush x:Key="NavigationViewItemForegroundSelectedPressed" Color="Red"/>
</Grid.Resources>
如果您希望将 NavigationViewItem 内容的初始状态和图标设置为红色,则必须在标记中进行设置。
这是用于填充 NavigationView.MenuItems
的项目列表 <NavigationView.MenuItems>
<NavigationViewItem Icon="AllApps" FontWeight="Bold" Foreground="Red" Content="Apps" Tag="apps"/>
<NavigationViewItem Icon="Video" FontWeight="Bold" Foreground="Red" Content="Games" Tag="games" />
<NavigationViewItem Icon="Audio" FontWeight="Bold" Content="Music" Tag="music"/>
</NavigationView.MenuItems>
结果:
通过此实现,前景 与边框/背景 分开,后者是负责表征 Reveal 功能的其他元素。
你可以修改上面提到的资源来实现你自己的 UI 逻辑,比我们现在实现的更“漂亮”,甚至可以使用绑定(bind)来改变运行时的主题,这取决于你的应用程序可能的自定义主题提供。
编辑: 要为所有 NavigationViewMenuItems(包括设置)设置前景属性,请覆盖以下资源:
<SolidColorBrush x:Key="NavigationViewItemForeground" Color="Red"/>
有了这个,您不需要为所有项目明确设置前景。使用 NavigationView 的 SettingsItem
属性,我只设法更改了图标颜色。
关于c# - NavigationView中如何设置文本的前景色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47823501/