您好,我有一个具有自定义样式的窗口(包括自定义模板),但现在我想在窗口模板布局中定义一些部分,例如 - 窗口标题栏、窗口内容(项目的默认位置)和窗口页脚,所以我可以像这样(或类似)使用它:
<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 设置这些属性的内容,那么,有没有办法实现这一点?
要启用示例的使用,您应该创建一个自定义基类,所有窗口都可以继承该基类。
定义基类的默认布局
SectionWindow
。只需在 /Themes/Generic.xaml文件中定义相关的默认
Style
即可:
Generic.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
依赖属性。因为默认 ContentPresenter
中的 Style
使用 ContentPresenter.ContentSource
属性,所以模板会自动映射。
对于
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>