如何在窗口栏中获取菜单?

spa*_*nki 2 c# wpf xaml window

我想知道如何在窗口栏中获取菜单,就像 Visual Studio 一样。

视觉工作室窗口

如果能够在左侧显示“文件”、“编辑”等按钮,在右侧显示标准的“最小化”、“最大化”和“关闭”按钮,那就太好了。这是可能吗?

我尝试Window WindowStyle="None"在栏中设置和添加自己的图标,但似乎不正确,但这是唯一的方法吗?

这就是我现在所拥有的。

我当前的窗口

<Window
        Title="MainWindow" 
        Height="{x:Static SystemParameters.PrimaryScreenHeight}"
        Width="{x:Static SystemParameters.PrimaryScreenWidth}"
        Closing="Window_Closing"
        WindowState="Maximized">
Run Code Online (Sandbox Code Playgroud)

Bio*_*ode 5

您必须使用以下类创建自定义窗口镶边WindowChrome

WPF 中的元素Window实际上托管在非 WPF(非客户端)主机中。该主机包括标题栏(标题)和标准按钮、图标和实际框架。这称为窗镀铬。

在此输入图像描述

通常您只能修改Window. 但在类的帮助下WindowChrome,WPF 允许客户区扩展到非客户区。
缺点是您必须基本上复制原始的非客户区域才能保留原始的外观和感觉。但毕竟您仍然会得到一些基本行为,例如双击框外时最大化窗口。

以下示例非常基本,但应该让您了解如何完成任务:

在此输入图像描述

我强烈建议您按照提供的课程链接WindowChrome并阅读备注部分,其中包含非常有价值的信息。您可以使用静态SystemParameters
类 提供的实际系统值来获取当前尺寸值,例如您应该在自定义镶边样式中使用的尺寸值。SystemParameters.WindowResizeBorderThickness

另请注意,要允许 WPF 捕获自定义 chrome 输入元素(如按钮或菜单)上的鼠标事件,您必须将WindowChrome.IsHitTestVisibleInChrome每个相关元素上的附加属性设置为true

WindowChrome.IsHitTestVisibleInChrome="True"
Run Code Online (Sandbox Code Playgroud)

创建上述视觉效果的基本样式如下:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:shell="clr-namespace:System.Windows.Shell;assembly=PresentationFramework">


  <Style x:Key="WindowStyle" TargetType="{x:Type Window}">
    <Setter Property="SnapsToDevicePixels" Value="True" />
    <Setter Property="WindowChrome.WindowChrome">
      <Setter.Value>
        <WindowChrome NonClientFrameEdges="Right"
                      ResizeBorderThickness="{x:Static SystemParameters.WindowResizeBorderThickness}" />
      </Setter.Value>
    </Setter>
    <Setter Property="Template">
      <Setter.Value>
        <ControlTemplate TargetType="{x:Type Window}">
          <Border Background="{TemplateBinding Background}"
                  BorderBrush="{TemplateBinding BorderBrush}"
                  BorderThickness="{TemplateBinding BorderThickness}">
            <Grid Background="Transparent">

              <AdornerDecorator>
                <Border Background="Transparent" Margin="{x:Static SystemParameters.WindowNonClientFrameThickness}">
                  <ContentPresenter />
                </Border>
              </AdornerDecorator>
              <ResizeGrip x:Name="WindowResizeGrip"
                          HorizontalAlignment="Right"
                          VerticalAlignment="Bottom"
                          Visibility="Collapsed"
                          IsTabStop="false" />
              <Grid Height="{Binding Source={x:Static SystemParameters.WindowNonClientFrameThickness}, Path=Top}"
                    Background="#FF3F3F3F"
                    VerticalAlignment="Top">
                <Grid.ColumnDefinitions>
                  <ColumnDefinition />
                  <ColumnDefinition Width="Auto" />
                </Grid.ColumnDefinitions>

                <!-- Custom window chrome -->
                <StackPanel Grid.Column="0" Orientation="Horizontal"
                            Margin="{x:Static SystemParameters.WindowResizeBorderThickness}">
                  <Image Source="{TemplateBinding Icon}" />
                  <TextBlock Text="{TemplateBinding Title}"
                             TextTrimming="CharacterEllipsis"
                             VerticalAlignment="Center"
                             Margin="16,0" />
                  <Menu shell:WindowChrome.IsHitTestVisibleInChrome="True">
                    <MenuItem Header="CustomChromeMenu">
                      <MenuItem Header="Action" />
                    </MenuItem>
                  </Menu>
                </StackPanel>

                <StackPanel Grid.Column="1"
                            Orientation="Horizontal"
                            HorizontalAlignment="Right">
                  <Button Width="45"
                          Height="{Binding Source={x:Static SystemParameters.WindowNonClientFrameThickness}, Path=Top}"
                          ToolTip="Minimize window"
                          ToolTipService.ShowDuration="5000"
                          shell:WindowChrome.IsHitTestVisibleInChrome="True">
                    <TextBlock Foreground="{Binding RelativeSource={RelativeSource AncestorType=Control}, Path=Foreground}"
               FontFamily="Segoe MDL2 Assets"
               FontSize="11"
               Text="&#xE921;" />
                  </Button>
                  <Button ToolTip="Maximize window"
                          Width="45"
                          Height="{Binding Source={x:Static SystemParameters.WindowNonClientFrameThickness}, Path=Top}"
                          ToolTipService.ShowDuration="5000"
                          shell:WindowChrome.IsHitTestVisibleInChrome="True">
                    <TextBlock Foreground="{Binding RelativeSource={RelativeSource AncestorType=Control}, Path=Foreground}"
               FontFamily="Segoe MDL2 Assets"
               FontSize="11"
               Text="&#xE922;" />
                  </Button>
                  <Button ToolTip="Close application"
                          ToolTipService.ShowDuration="5000"
                          Width="50"
                          Height="{Binding Source={x:Static SystemParameters.WindowNonClientFrameThickness}, Path=Top}"
                          shell:WindowChrome.IsHitTestVisibleInChrome="True">
                    <TextBlock Foreground="{Binding RelativeSource={RelativeSource AncestorType=Control}, Path=Foreground}"
               FontFamily="Segoe MDL2 Assets"
               FontSize="11"
               Text="&#xE8BB;" />
                  </Button>
                </StackPanel>
              </Grid>
            </Grid>
          </Border>
          <ControlTemplate.Triggers>
            <Trigger Property="ResizeMode"
                     Value="CanResizeWithGrip">
              <Setter TargetName="WindowResizeGrip"
                      Property="Visibility"
                      Value="Visible" />
            </Trigger>
          </ControlTemplate.Triggers>
        </ControlTemplate>
      </Setter.Value>
    </Setter>
  </Style>
</ResourceDictionary>
Run Code Online (Sandbox Code Playgroud)