wpf - 自定义 WPF 窗口样式

标签 wpf xaml window contentpresenter

我正在尝试制作自定义窗口样式。目标是创建一个模板,我的应用程序中的每个窗口都可以使用该模板。模板包含工具栏、标题和“窗口将使用的区域”。问题是:当我使用我的样式时,我无法再添加网格和控件。

应用程序.xaml

<Style x:Key="CustomWindowStyle" TargetType="{x:Type Window}">
  <Setter Property="WindowStyle" Value="None"/>
  <Setter Property="AllowsTransparency" Value="True"/>
  <Setter Property="ResizeMode" Value="NoResize"/>
  <Setter Property="Background" Value="MintCream"/>
  <Setter Property="BorderBrush" Value="#0046E7"/>
  <Setter Property="BorderThickness" Value="2"/>
  <Setter Property="Template">
    <Setter.Value>
      <ControlTemplate TargetType="{x:Type Window}">
        <Grid Background="{TemplateBinding Background}">
          <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition/>
          </Grid.RowDefinitions>
          <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition Width="Auto"/>
          </Grid.ColumnDefinitions>
          <StackPanel Grid.ColumnSpan="2">
            <TextBlock TextAlignment="Center"
                       Margin="0 10 0 0"
                       FontSize="22"
                       FontWeight="DemiBold"
                       Foreground="RoyalBlue"
                       Text="{TemplateBinding Title}"/>
          </StackPanel>
          <StackPanel Grid.Row="0" Grid.Column="1"
                      Orientation="Horizontal"
                      HorizontalAlignment="Stretch"
                      VerticalAlignment="Center"
                      Margin="0 10 15 0">
            <Button Style="{StaticResource MinimizeButtonStyle}"
                    Width="25"
                    Height="22"
                    Margin="0 0 10 0"/>
            <Button Style="{StaticResource CloseButtonStyle}"
                    Width="25"
                    Height="22"/>
          </StackPanel>
        </Grid>
      </ControlTemplate>
    </Setter.Value>
  </Setter>
</Style>

主窗口.xaml
<Window x:Class="WindowForHW2.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:WindowForHW2"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525"
    Style="{StaticResource CustomWindowStyle}">
<Grid>
  <Button Width="100" Height="40" Content="Hello"/>
</Grid>

模板有效,但我不能再添加 smth:
Window:

最佳答案

您需要在 ContentPresenterContent 所在的位置添加 Window。试试这个。

<Style x:Key="CustomWindowStyle" TargetType="{x:Type Window}">
  <Setter Property="WindowStyle" Value="None"/>
  <Setter Property="AllowsTransparency" Value="True"/>
  <Setter Property="ResizeMode" Value="NoResize"/>
  <Setter Property="Background" Value="MintCream"/>
  <Setter Property="BorderBrush" Value="#0046E7"/>
  <Setter Property="BorderThickness" Value="2"/>
  <Setter Property="Template">
    <Setter.Value>
      <ControlTemplate TargetType="{x:Type Window}">
        <Grid Background="{TemplateBinding Background}">
          <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
          </Grid.RowDefinitions>
          <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition Width="Auto"/>
          </Grid.ColumnDefinitions>
          <StackPanel Grid.ColumnSpan="2">
            <TextBlock TextAlignment="Center"
                       Margin="0 10 0 0"
                       FontSize="22"
                       FontWeight="DemiBold"
                       Foreground="RoyalBlue"
                       Text="{TemplateBinding Title}"/>
          </StackPanel>
          <StackPanel Grid.Row="0" Grid.Column="1"
                      Orientation="Horizontal"
                      HorizontalAlignment="Stretch"
                      VerticalAlignment="Center"
                      Margin="0 10 15 0">
            <Button Content="+"
                    Width="25"
                    Height="22"
                    Margin="0 0 10 0"/>
            <Button Content="X"
                    Width="25"
                    Height="22" />
          </StackPanel>
        <!-- here goes the content -->
        <ContentPresenter Grid.Row="1"/>
       </Grid>
      </ControlTemplate>
    </Setter.Value>
  </Setter>
</Style>

关于wpf - 自定义 WPF 窗口样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39882290/

相关文章:

c# - XAML ListBox 只显示类名

wpf - 将鼠标悬停在 Silverlight 中的对象上时显示自定义工具提示?/弹出窗口

c# - 如何处理项目计数检查器

java - 制作一个简单的 Java OptionPane 计数器

java - 是否可以检测窗口遮挡

c# - 如何在 wpf 中使用没有 Windows Aero 玻璃效果的 WindowChrome,黑色边框

c# - 从 StaticResource 设置 RowDefinition 高度

wpf - 图像以适应 WPF 中的网格单元格大小

wpf - 如何使用 Resharper 使 XAML 编辑更具响应性

macos - SwiftUI:关闭 macOS 上打开的窗口会导致崩溃