【问题标题】:Show default window buttons on WPF Window using WindowChrome使用 WindowChrome 在 WPF 窗口上显示默认窗口按钮
【发布时间】:2025-12-15 17:25:01
【问题描述】:

我发现在 WPF 窗口上使用 WindowChrome 可以让我将内容放在窗口中任何我想要的位置,这是一个很棒的想法。现在我终于可以让标题栏与我的应用程序的其余部分具有相同的颜色了。

但是,我仍然喜欢出现在非样式窗口上的默认最小化/最大化/关闭按钮。

有什么方法可以让我使用 WindowChrome 设置我的 Window 样式但仍保留默认按钮?

<Style x:Key="MainWindow" TargetType="{x:Type Window}">
    <Setter Property="WindowChrome.WindowChrome">
        <Setter.Value>
            <WindowChrome CaptionHeight="30" ResizeBorderThickness="5" />
        </Setter.Value>
    </Setter>
</Style>

<Window Style="{StaticResource MainWindow}">
    <Grid Background="Black">
        <MyContent />
    </Grid>
</Window>

这会产生一个黑色的大窗口(很棒!),但没有任何最小/最大/关闭按钮,因为它们隐藏在 Grid 后面(不太好)。不过,这些按钮仍然是可点击的,所以它们显然就在那里。

【问题讨论】:

    标签: wpf xaml window-chrome


    【解决方案1】:

    有什么方法可以让我使用 WindowChrome 设置我的 Window 样式但仍保留默认按钮?

    不,我不这么认为。您可以将GlassFrameThickness 属性设置为0 以禁用按钮,然后恐怕您必须创建自己的自定义按钮。您可以参考以下项目的一些示例:https://wpfwindow.codeplex.com/

    如果您想要 Window 10 样式的标题按钮,您可以使用 Segoe MDL2 Assets 字体系列:

    <Style x:Key="CaptionButtonStyle" TargetType="Button">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <Grid x:Name="LayoutRoot" Background="Transparent" Width="44" Height="30">
                        <TextBlock x:Name="txt" Text="{TemplateBinding Content}" FontFamily="Segoe MDL2 Assets" FontSize="10" 
                                       Foreground="#999999" HorizontalAlignment="Center" VerticalAlignment="Center"
                                       RenderOptions.ClearTypeHint="Auto" TextOptions.TextRenderingMode="Aliased"  TextOptions.TextFormattingMode="Display"/>
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter TargetName="LayoutRoot" Property="Background" Value="#E5E5E5"/>
                            <Setter TargetName="txt" Property="Foreground" Value="#000000"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    
    <Style x:Key="MinimizeButtonStyle" TargetType="Button" BasedOn="{StaticResource CaptionButtonStyle}">
        <Setter Property="Content" Value="&#xE949;"/>
    </Style>
    
    <Style x:Key="MaximizeButtonStyle" TargetType="Button" BasedOn="{StaticResource CaptionButtonStyle}">
        <Setter Property="Content" Value="&#xE739;"/>
    </Style>
    
    <Style x:Key="RestoreButtonStyle" TargetType="Button" BasedOn="{StaticResource CaptionButtonStyle}">
        <Setter Property="Content" Value="&#xE923;"/>
    </Style>
    

    【讨论】:

    • 谢谢。我不知道Segoe MDL2 Assets,这是一个很大的帮助:)
    • 谢谢!以供将来参考,关闭它是 。
    【解决方案2】:

    在引入 Windows 10 之前,您可以通过在外部 Border 上添加 Margin 属性来自定义您的 Window.Template。就像下面的代码一样。

    <Window.Template>
        <ControlTemplate TargetType="Window">
            <Border Background="#3FFFFFFF">
                <AdornerDecorator Margin="8 10 8 8">
                    <Border>
                        <ContentPresenter/>
                    </Border>
                </AdornerDecorator>
            </Border>
        </ControlTemplate>
    </Window.Template>
    <WindowChrome.WindowChrome>
        <WindowChrome UseAeroCaptionButtons="True" GlassFrameThickness="8 30 8 8"/>
    </WindowChrome.WindowChrome>
    

    上面的代码做了这些事情:

    1. 使用WindowChrome 使我们的控件在窗口上方显示为无客户区。
    2. 避免我们的控件覆盖窗口标题按钮。
    3. 如果您想在标题栏上方显示一些控件,请将其 Margin 属性设置为负数,例如 0 -30 0 0

    但在 Windows 10 之后,尤其是 Windows 10 创意者更新,您无法再以可接受的窗口样式显示窗口默认按钮。它总是会显示一个主题边框,丑陋!

    <WindowChrome NonClientFrameEdges="Left,Bottom,Right" UseAeroCaptionButtons="True" GlassFrameThickness="8 30 8 8"/>
    

    上面的代码可能适用于早期版本的 Windows 10。我的意思是,不像 Creators Update 那样丑陋。

    【讨论】: