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

使用WindowChrome时自定义窗口栏按钮无响应的解决方法

自定义WPF窗口栏时WindowChrome导致顶部按钮无响应的解决办法

在VSCode 2022中开发WPF应用时,为制作自定义窗口栏并保留窗口拖拽等常规窗口功能,使用了WindowChrome类,但遇到顶部按钮无响应的问题——窗口顶部区域被赋予拖拽功能后,按钮无法接收点击事件。曾尝试自行实现默认窗口功能,但无法让所有特性正常工作。

相关代码

XAML代码

<Window x:Class="ConsoleWindow_v0._0._1.MainWindow"
        xmlns:System="clr-namespace:System;assembly=mscorlib"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:shell="http://schemas.microsoft.com/winfx/2006/xaml/presentation/shell"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:ConsoleWindow_v0._0._1"
        mc:Ignorable="d"
        WindowStyle="None"
        Height="450" Width="800">

    <Window.Resources>
       ...
    </Window.Resources>


    <Grid x:Name="ParentGrid"
          Margin="0, 0, 0, 0"
          Background="#191919">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="30"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="1"/>
            <RowDefinition Height="30"/>
        </Grid.RowDefinitions>

        <!--Window Handle-->
        <Grid x:Name="ChildGrid_1"
              Grid.Row="0"
              Margin="3,3,3,0"
              VerticalAlignment="Stretch"
              HorizontalAlignment="Stretch">

            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>

            <Grid.RowDefinitions>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>

            <!--System Buttons-->
            <Grid x:Name="SystemButtons"
                        Grid.Column="1"
                        Background="#1f1f1f"
                        VerticalAlignment="Center"
                        HorizontalAlignment="Right">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>

                <Button x:Name="MinimizeButton"
                        Grid.Column="0"
                        HorizontalAlignment="Stretch"
                        KeyboardNavigation.IsTabStop="False"
                        Click="MinimizeButton_Click"
                        Content="-"
                        Style="{StaticResource SystemButtonStyle}"/>

                <Button x:Name="MaximizeButton"
                        Grid.Column="1"
                        HorizontalAlignment="Stretch"
                        KeyboardNavigation.IsTabStop="False"
                        Click="MaximizeButton_Click"
                        Content="□"
                        Style="{DynamicResource SystemButtonStyle}" />

                <Button x:Name="CloseButton"
                        Grid.Column="2"
                        HorizontalAlignment="Stretch"
                        KeyboardNavigation.IsTabStop="False"
                        Content="x"
                        Click="CloseButton_Click"
                        Style="{DynamicResource SystemButtonStyle}"/>

            </Grid>

            <!--Window Title-->
            <StackPanel x:Name="HandleBar"
                  Background="#1f1f1f"
                  Grid.Column="0"
                  Orientation="Horizontal">

                <Grid x:Name="g"
                      Grid.Column="0"
                      Grid.Row="0"
                      Width="{StaticResource WindowHandle_Height}"
                      Height="{StaticResource WindowHandle_Height}"
                      HorizontalAlignment="Left"
                      VerticalAlignment="Center">

                    <Image x:Name="Icon" 
                           HorizontalAlignment="Stretch"
                           VerticalAlignment="Stretch"                           
                           Width="Auto"
                           Height="Auto"
                           Opacity="0.65"
                           Source="/Zeichnung.png"/>
                </Grid>

                <Button x:Name="DateiButton"
                        Grid.Column="1"
                        Grid.Row="0"
                        Style="{StaticResource UserButtons}"
                        Content="Datei"/>

                <Button x:Name="SettingsButton"
                        Grid.Column="1"
                        Grid.Row="0"
                        Style="{StaticResource UserButtons}"
                        Content="Settings"/>

                <Button x:Name="AnsichtButton"
                        Grid.Column="2"
                        Grid.Row="0"
                        Style="{StaticResource UserButtons}"
                        Content="Ansicht"/>

            </StackPanel>

            <!--Window Handle-->

        </Grid>

        <!--Client Area-->
        <Grid x:Name="ChildGrid_2"
              Grid.Row="1">
            ...

            </Grid>
        </Grid>

        <!--Quick Settings-->
        <Grid x:Name="QuickSettingsGrid"
              Grid.Row="3"
              Margin="3,3,3,3">
                ...
        </Grid>


    </Grid>

</Window>

后台代码(Code Behind)

public MainWindow() {
            InitializeComponent();
            WindowChrome.SetWindowChrome(this, new WindowChrome());
            WindowChrome.IsHitTestVisibleInChromeProperty = true;

            ChildGrid_1.ColumnDefinitions[1].Width = new GridLength(3 * systemButton_size);
        }

解决方案

针对通过后台代码操作WindowChrome遇到该问题的开发者,可遍历顶部按钮容器内的所有元素,为每个元素设置WindowChrome.SetIsHitTestVisibleInChrome为true,确保这些元素能正常接收点击事件。

修改后的后台代码:

InitializeComponent();
foreach (UIElement element in SystemButtons.Children) {
    WindowChrome.SetIsHitTestVisibleInChrome(element, true);
}
foreach (UIElement element in HandleBar.Children) {
    WindowChrome.SetIsHitTestVisibleInChrome(element, true);
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.27 07:35:02