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

WPF中使用Material Design包时自定义NavButton控件消失问题

WPF自定义NavButton引入Material Design后消失问题

我在WPF中创建了名为NavButton的自定义按钮控件,将其作为侧边栏导航按钮放入MainWindow的ListBox中。未引入Material Design包时控件显示正常,但在App.xaml中添加该包的主题配置后,控件突然消失。已确认AssemblyInfo.cs中包含ResourceDictionaryLocation.None和ResourceDictionaryLocation.SourceAssembly配置。

相关代码

App.xaml

<Application x:Class="Nav.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:SideBar_Nav"
             xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
             StartupUri="MainWindow.xaml">

    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <materialDesign:BundledTheme BaseTheme="Light" PrimaryColor="DeepPurple" SecondaryColor="Lime" />
                <ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Defaults.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

Generic.xaml(自定义控件模板)

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
    xmlns:local="clr-namespace:Nav">


    <Style TargetType="{x:Type local:NavButton}">

        <Setter Property="HorizontalAlignment" Value="Center"/>
        <Setter Property="Cursor" Value="Hand"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:NavButton}">
                    <Border x:Name="back" CornerRadius="8" Padding="{TemplateBinding Padding}" Background="Transparent" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}">
                        <Path x:Name="icon" Height="18" Width="18" Stretch="Uniform" Fill="#7b8792" Data="{TemplateBinding Icon}"/>
                    </Border>

                    <ControlTemplate.Triggers>

                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="Background" TargetName="back" Value="#d0ebff"/>
                            <Setter Property="Fill" TargetName="icon" Value="#2a84f1"/>
                        </Trigger>

                        <Trigger Property="IsSelected" Value="True">
                            <Setter Property="Background" TargetName="back" Value="#d0ebff"/>
                            <Setter Property="Fill" TargetName="icon" Value="#2a84f1"/>
                        </Trigger>

                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

AssemblyInfo.cs相关配置

[assembly: ThemeInfo(
    ResourceDictionaryLocation.None,
    ResourceDictionaryLocation.SourceAssembly
)]

解决方法

  1. 隔离自定义控件样式继承
    Material Design的全局Button样式会自动被自定义按钮继承,导致原有模板被覆盖。修改Generic.xaml中的Style,明确禁止继承:

    <Style TargetType="{x:Type local:NavButton}" BasedOn="{x:Null}">
    
  2. 重置ListBox项容器样式
    Material Design会默认修改ListBoxItem的布局(如添加内边距、背景色),可能导致NavButton被挤压或遮挡。为ListBox显式设置ItemContainerStyle:

    <ListBox>
        <ListBox.ItemContainerStyle>
            <Style TargetType="ListBoxItem">
                <Setter Property="Padding" Value="0"/>
                <Setter Property="BorderThickness" Value="0"/>
                <Setter Property="Background" Value="Transparent"/>
                <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
                <Setter Property="VerticalContentAlignment" Value="Center"/>
            </Style>
        </ListBox.ItemContainerStyle>
        <!-- NavButton 项定义 -->
    </ListBox>
    
  3. 确认资源字典加载有效性
    检查Generic.xaml的“生成操作”是否设置为Resource(右键文件→属性→生成操作),确保资源能被正确嵌入到程序集内。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.01 13:06:08