You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WPFUI导航栏图标未正常渲染,仅鼠标悬停时显示

WPF UI导航栏图标仅悬停显示问题解决方案

问题根源

在ViewModel中直接实例化SymbolIcon这类UI控件时,控件未正确纳入WPF视觉树,导致默认样式(包括图标前景色)未正常应用,只有鼠标悬停触发样式触发器时图标才会显示。同时WPF-UI 3.0.0-preview4预览版存在此类绑定兼容性问题。

解决方案

方案1:使用NavigationViewItemModel(推荐,符合MVVM规范)

WPF-UI官方推荐用数据模型替代在ViewModel中创建UI控件,NavigationView会自动将模型转换为对应的NavigationViewItem并正确应用样式:

修改MainWindowViewModel.cs中的菜单集合定义:

using Wpf.Ui.Common;
using Wpf.Ui.Controls;
using Wpf.Ui.Mvvm;

[ObservableProperty]
private ObservableCollection<NavigationViewItemModel> _menuItems = new()
{
    new NavigationViewItemModel()
    {
        Content = "MENU PRINCIPAL",
        IconSymbol = SymbolRegular.Home24,
        TargetPageType = typeof(Views.Pages.DashboardPage)
    },
    new NavigationViewItemModel()
    {
        Content = "LISTA CHOFERES",
        IconSymbol = SymbolRegular.ClipboardTaskListLtr24,
        TargetPageType = typeof(Views.Pages.ChoferDataPage)
    },
    // 其余菜单项按此格式修改
};

[ObservableProperty]
private ObservableCollection<NavigationViewItemModel> _footerMenuItems = new()
{
    new NavigationViewItemModel()
    {
        Content = "CONFIGURACION",
        IconSymbol = SymbolRegular.Settings24,
        TargetPageType = typeof(Views.Pages.SettingsPage)
    }
};

方案2:手动绑定SymbolIcon前景色

如果坚持在ViewModel中创建NavigationViewItem,需为SymbolIcon绑定动态资源前景色,确保默认状态下显示:

new NavigationViewItem()
{
    Content = "MENU PRINCIPAL",
    Icon = new SymbolIcon { 
        Symbol = SymbolRegular.Home24,
        Foreground = (Brush)new DynamicResourceExtension("TextFillColorPrimaryBrush")
    },
    TargetPageType = typeof(Views.Pages.DashboardPage)
}

验证步骤

  1. 清理并重建项目
  2. 运行应用,检查导航栏图标是否默认显示
  3. 若仍有问题,确认项目App.xaml中已正确引入WPF-UI的资源字典

内容的提问来源于stack exchange,提问作者ISeeMangos

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.11 05:52:15