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

统一NavigationView面板与内容背景色,解决展开面板透明失效问题

解决NavigationView展开面板背景透明问题

你遇到的核心问题是:WinUI的NavigationView默认模板中,展开面板的背景并未直接响应NavigationViewExpandedPaneBackground资源设置,或者资源作用域优先级不足。以下是两种可靠的解决方法:

方案1:覆盖完整NavigationView模板(最彻底)

通过提取默认模板并修改对应背景元素,直接控制展开面板的透明度:

  1. 在Blend中打开项目,右键选中NavigationView,选择编辑模板→编辑副本,生成完整的模板代码。
  2. 在生成的模板里找到ExpandedPaneGrid(展开面板的背景容器),将其Background设为Transparent,或添加Opacity="0"。
  3. 保存模板后,展开面板背景即可变为透明。

方案2:调整资源作用域并补充关键资源

无需修改完整模板,只需将资源提升到Window级别,并补充覆盖NavigationViewPaneBackground资源:

<?xml version="1.0" encoding="utf-8"?>
<winex:WindowEx
    xmlns:winex="using:WinUIEx"
    Width="800"
    Height="500"
    MinHeight="500"
    MinWidth="800"
    x:Class="Hostware.Hostware"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:Hostware"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <!-- 将资源移至Window级别,提升优先级 -->
    <winex:WindowEx.Resources>
        <Thickness x:Key="NavigationViewContentMargin">0,48,0,0</Thickness>
        <Thickness x:Key="NavigationViewMinimalContentMargin">0,48,0,0</Thickness>
        <Thickness x:Key="NavigationViewContentGridBorderThickness">1,1,0,0</Thickness>
        <SolidColorBrush x:Key="NavigationViewContentBackground" Color="Transparent" />
        <SolidColorBrush x:Key="NavigationViewExpandedPaneBackground" Color="Transparent" />
        <SolidColorBrush x:Key="NavigationViewPaneBackground" Color="Transparent" />
        <CornerRadius x:Key="NavigationViewContentGridCornerRadius">8,8,8,8</CornerRadius>
    </winex:WindowEx.Resources>

    <StackPanel Background="Orange">
        <VisualStateManager.VisualStateGroups>
            <VisualStateGroup>
                <VisualState>
                    <VisualState.StateTriggers>
                        <!--<AdaptiveTrigger MinWindowWidth="{x:Bind nvSample2.CompactModeThresholdWidth}" /> -->
                    </VisualState.StateTriggers>
                    <VisualState.Setters>
                        <Setter Target="sidebar.PaneDisplayMode" Value="LeftCompact"/>
                    </VisualState.Setters>
                </VisualState>
            </VisualStateGroup>
        </VisualStateManager.VisualStateGroups>
        
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
            </Grid.RowDefinitions>

            <NavigationView
                x:Name="sidebar"
                Grid.Row="1" Height="485"
                Header="This is Header Text"
                Margin="0" PaneDisplayMode="LeftCompact"
                IsTabStop="False" AlwaysShowHeader="False"
                Background="Transparent" <!-- 移除原有Orange背景避免干扰 -->
                SelectionChanged="NavigationView_SelectionChanged2"
                OpenPaneLength="150"
                Grid.RowSpan="2"
                IsPaneOpen="True"
                Canvas.ZIndex="0"
                Grid.Column="1"
                IsTitleBarAutoPaddingEnabled="True"
                IsBackButtonVisible="Collapsed">

                <NavigationView.MenuItems>
                    <NavigationViewItem Content="Menu Item1" Tag="SamplePage1">
                        <NavigationViewItem.Icon>
                            <ImageIcon Source="\Assets\icon_tf.png"></ImageIcon>
                        </NavigationViewItem.Icon>
                    </NavigationViewItem>
                    <NavigationViewItem Content="Menu Item2" Tag="SamplePage2" Icon="Save" />
                    <NavigationViewItem Content="Menu Item3" Tag="SamplePage3" Icon="Refresh" />
                    <NavigationViewItem Content="Menu Item4" Tag="SamplePage4" Icon="Download" />
                </NavigationView.MenuItems>
                
                <Frame Margin="0,0,0,0" x:Name="contentFrame5" Background="white" VerticalAlignment="Stretch" HorizontalAlignment="Stretch"/>
            </NavigationView>
        </Grid>
    </StackPanel>
</winex:WindowEx>

额外提示

  • 确保使用最新版WinUI,旧版本可能存在资源引用bug。
  • 如果自定义了视觉状态,需检查状态设置是否覆盖了背景属性。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.20 09:39:54