I have a class EmptyWindow
which inherits from WPF Window
. It has no XAML and is only used to set WindowStyle = None
to remove Windows OS header.
I have bunch of windows that inherits from EmptyWindow
. I want to emulate header without copy/pasting same code 50 times.
I want a dependency/attached property EmptyWindow.TitleContent
that would place text on the top of the window.
So the usage in the children would be EmptyWindow.TitleContent="A Child Title"
and the actual child content would go into EmptyWindow.Content
If it were a custom ContentControl
I'd supply default template for it with TitleContent
to be bound to ContentPresenter
in a grid row 0 and Content
in row 1.
I cant quite figure how to make the same in windows.
What have I done (and it is not working - the inherited window is supposed to display "A Child Title" string on the top and "A WINDOW" in the middle, I have empty black window)
App.xaml
<Application x:Class="WindowSubclassing.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WindowSubclassing"
StartupUri="MainWindow.xaml">
<Application.Resources>
<Style TargetType="local:SlimWindow" BasedOn="{StaticResource {x:Type Window}}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:SlimWindow}">
<Grid Background="Blue">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<ContentPresenter ContentSource="TitleContent" />
<AdornerDecorator Grid.Row="1">
<ContentPresenter/>
</AdornerDecorator>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Application.Resources>
</Application>
the base window class:
public class SlimWindow : Window
{
static SlimWindow()
{
var metaData = new FrameworkPropertyMetadata(typeof(SlimWindow));
DefaultStyleKeyProperty.OverrideMetadata(typeof(SlimWindow), metaData);
}
public object TitleContent
{
get => GetValue(TitleContentProperty);
set => SetValue(TitleContentProperty, value);
}
public static readonly DependencyProperty TitleContentProperty = DependencyProperty.Register(
nameof(TitleContent), typeof(object), typeof(SlimWindow));
}
the final window:
<local:SlimWindow x:Class="WindowSubclassing.AWindow"
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:WindowSubclassing"
mc:Ignorable="d"
Title="AWindow" Height="450" Width="800" TitleContent="A Child Title">
<Grid>
<TextBlock Text="A WINDOW" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Grid>
</local:SlimWindow>