XAML 自定义样式窗口的内容部分

pprl5pva  于 2024-01-04  发布在  其他
关注(0)|答案(2)|浏览(175)

问候,我有一个自定义样式的窗口(包括自定义模板),但现在我想在我的窗口模板布局中定义一些部分,例如-窗口标题栏,窗口内容(项目的默认位置)和窗口页脚,所以我可以这样使用它(或类似):

<Window Style="{StaticResource CustomWindow}">
    <Window.Header>
        <Grid x:Name="HeaderGrid">
        </Grid>
    </Window.Header>
    <Grid x:Name="ContentGrid">
    </Grid>
    <Window.Footer>
        <Grid x:Name="FooterGrid">
        </Grid>
    </Window.Footer>
</Window>

字符串
我想我必须添加相应的依赖属性到包含我的窗口样式的样式字典的. xaml.cs文件中,但它的类不是从任何东西继承的,所以它不是DependencyObject,我试图添加依赖属性到使用样式的窗口中,所以我可以将模板中的ContentPresenters绑定到它,但它不允许从窗口的xaml设置这些属性的内容,那么有没有办法做到呢?

rlcwz9us

rlcwz9us1#

要使用你的例子,你应该创建一个自定义的基类,你的所有窗口都可以从它继承。
定义基类SectionWindow的默认布局。只需在 /Themes/Generic.xaml 文件中定义相关的默认Style

泛型.xaml

<Style TargetType="{x:Type local:SectionWindow}">
  <Setter Property="HorizontalContentAlignment"
          Value="Stretch" />
  <Setter Property="VerticalContentAlignment"
          Value="Stretch" />
  <Setter Property="Template">
    <Setter.Value>
      <ControlTemplate TargetType="local:SectionWindow">
        <Border Background="{TemplateBinding Background}"
                BorderBrush="{TemplateBinding BorderBrush}"
                BorderThickness="{TemplateBinding BorderThickness}"
                Padding="{TemplateBinding Padding}">
          <Grid>
            <Grid.RowDefinitions>
              <!-- Dynamic content header row -->
              <RowDefinition Height="Auto" />
              <!-- Dynamic content row -->
              <RowDefinition />
              <!-- Dynamic content footer row -->
              <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>

            <ContentPresenter Grid.Row="0"
                              ContentSource="Header"
                              HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                              VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />

            <ContentPresenter Grid.Row="1"
                              ContentSource="Content"
                              HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                              VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />

            <ContentPresenter Grid.Row="2"
                              ContentSource="Footer"
                              HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                              VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
          </Grid>
        </Border>
      </ControlTemplate>
    </Setter.Value>
  </Setter>
</Style>

字符串
然后定义基类。这是一个基本的例子。建议为每个节属性定义相应的模板属性。例如,Header属性应该有一个关联的HeaderTemplate依赖属性,类型为DataTemplate。由于默认Style中的ContentPresenter使用ContentPresenter.ContentSource属性,因此模板将自动Map。
对于Content属性,我们可以重用继承的Window.Content属性。

SectionWindow.cs

public class SectionWindow : Window
{
  public object Header
  {
    get => (object)GetValue(HeaderProperty);
    set => SetValue(HeaderProperty, value);
  }

  public static readonly DependencyProperty HeaderProperty = DependencyProperty.Register(
    "Header", 
    typeof(object), 
    typeof(SectionWindow), 
    new PropertyMetadata(default));

  public object Footer
  {
    get => (object)GetValue(FooterProperty);
    set => SetValue(FooterProperty, value);
  }

  public static readonly DependencyProperty FooterProperty = DependencyProperty.Register(
    "Footer",
    typeof(object),
    typeof(SectionWindow),
    new PropertyMetadata(default));

  static SectionWindow() => DefaultStyleKeyProperty.OverrideMetadata(typeof(SectionWindow), new FrameworkPropertyMetadata(typeof(SectionWindow)));
}


然后,您可以使用新窗口SectionWindow,如下所示:

MainWindow.xaml.cs

public partial class MainWindow : SectionWindow
{
  public ExampleWindow() 
  {
    InitializeComponent();
  }
}

MainWindow.xaml

<local:SectionWindow x:Class="Net.Wpf.MainWindow" ...>
  <local:SectionWindow.Header>
    <Border Background="Red">
      <TextBlock Text="Header content" />
    </Border>
  </local:SectionWindow.Header>

  <local:SectionWindow.Content>
    <Border Background="Green">
      <TextBlock Text="Window content" />
    </Border>
  </local:SectionWindow.Content>

  <local:SectionWindow.Footer>
    <Border Background="Blue">
      <TextBlock Text="Footer content" />
    </Border>
  </local:SectionWindow.Footer>
</local:SectionWindow>

kkih6yb8

kkih6yb82#

您可以创建从Window派生的CustomControl,如下所示

public class CustomWindow : Window
{
    public static readonly DependencyProperty HeaderProperty = DependencyProperty.Register("Header", typeof(object), typeof(CustomWindow));
    public object Header
    {
        get { return (object)GetValue(HeaderProperty); }
        set { SetValue(HeaderProperty, value); }
    }

    public static readonly DependencyProperty FooterProperty = DependencyProperty.Register("Footer", typeof(object), typeof(CustomWindow));
    public object Footer
    {
        get { return (object)GetValue(FooterProperty); }
        set { SetValue(FooterProperty, value); }
    }

    static CustomWindow()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomWindow), new FrameworkPropertyMetadata(typeof(CustomWindow)));
    }
}

字符串
Style表示,

<Style TargetType="{x:Type local:CustomWindow}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:CustomWindow}">
                <Border Background="{TemplateBinding Background}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}">
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="100"/>
                            <RowDefinition/>
                            <RowDefinition Height="100"/>
                        </Grid.RowDefinitions>

                        <!-- Header -->
                        <ContentPresenter Content="{TemplateBinding Header}"/>

                        <!-- Content -->
                        <ContentPresenter Grid.Row="1" Content="{TemplateBinding Content}"/>

                        <!-- Footer -->
                        <ContentPresenter Grid.Row="2" Content="{TemplateBinding Footer}"/>
                    </Grid>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>


这里是用法示例

<local:CustomWindow x:Class="WpfApp10.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:WpfApp10"
                    Title="MainWindow"
                    Width="800"
                    Height="450"
                    mc:Ignorable="d">
    <local:CustomWindow.Header>
        <Rectangle Fill="Red"/>
    </local:CustomWindow.Header>

    <local:CustomWindow.Footer>
        <Rectangle Fill="Blue"/>
    </local:CustomWindow.Footer>

    <Rectangle Fill="Green"/>
</local:CustomWindow>


不要忘记将MainWindow.xaml.cs的基类更改为CustomWindow

相关问题