wpf 使用Window Chrome时向窗口栏添加按钮

p8h8hvxi  于 2023-03-24  发布在  其他
关注(0)|答案(1)|浏览(199)

我正在VSCode 2022中使用WPF开发一个应用程序。为了制作一个自定义窗口栏,同时保持正常的窗口功能(即拖动窗口),我决定使用WindowChrome类。
但现在我面临的问题是,顶部的按钮不再有React了,因为窗口的顶部现在有了拖动窗口的功能。
我想找到一个变通办法,让我必须默认窗口功能,并让我自定义窗口栏。
示例:

<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>

代码隐藏:

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

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

我已经尝试过自己实现默认的窗口函数,但我不能让所有的功能都工作。

Sulotion对于任何有同样问题并且正在从Code Behind使用Window Chrome的人,以下是我如何解决的(基于@emoacht的答案):

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

dm7nw8vv1#

您误解了WindowChrome.IsHitTestVisibleInChrome附加属性的用法。它应该在CaptionHeight覆盖的每个控件中设置,但需要进行命中测试可见。
在这种情况下,您可以将以下内容插入到SystemButtonStyle中。

<Setter Property="WindowChrome.IsHitTestVisibleInChrome" Value="True"/>

相关问题