Search code examples
c#wpfxamlwpf-controlscontentcontrol

Overrriding content of a custom WPF Window


I created this TinyWindow class which derives from Window.

public class TinyWindow : Window
{
    public TinyWindow()
    {
        Grid grid = new Grid();
        grid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(50, GridUnitType.Pixel) });
        grid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1, GridUnitType.Star) });
                        
        ContentControl contentControl = new ContentControl();
        contentControl.SetBinding(ContentControl.ContentProperty, new Binding(nameof(WindowContent)) { Source = this });
        Grid.SetRow(contentControl, 1);
        grid.Children.Add(contentControl);

        Content = grid;     
        
    }
    
    public object WindowContent
    {
        get { return (object)GetValue(WindowContentProperty); }
        set { SetValue(WindowContentProperty, value); }
    }

    // Using a DependencyProperty as the backing store for WindowContent.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty WindowContentProperty =
        DependencyProperty.Register("WindowContent", typeof(object), typeof(TinyWindow), new PropertyMetadata(null));
    }
}

So I can use it like this in XAML.

<controls:TinyWindow x:Class="MyProject.Windows.SomeTinyWindow"
    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:MyProject.Windows"
    xmlns:controls="clr-namespace:MyProject.Controls"
    mc:Ignorable="d"
    Title="Select a Port">
<controls:TinyWindow.WindowContent>
   <TextBlock Text="this is my content"/>
</controls:TinyWindow.WindowContent>

Which works fine. But I want to achieve the same result writing only this.

<controls:TinyWindow x:Class="MyProject.Windows.SomeTinyWindow"
    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:MyProject.Windows"
    xmlns:controls="clr-namespace:MyProject.Controls"
    mc:Ignorable="d"
    Title="Select a Port">
<TextBlock Text="this is my content"/>

How can I achieve that? Overriding Content property? I tried that but base.Content is null here.

public new object Content
{
    get { return base.Content; }
    set
    {
        var grid = base.Content as Grid;
        //some logic to add value to gris
    }
}

What do you recommend?


Solution

  • You do not need to declare a derived Window class with an additional Content property. Just declare a Window ControlTemplate resource, e.g. in Application.Resources

    <ControlTemplate x:Key="TinyWindowTemplate" TargetType="Window">
        <Grid Background="{TemplateBinding Background}">
            <Grid.RowDefinitions>
                <RowDefinition Height="50"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
            <ContentPresenter Grid.Row="1" Content="{TemplateBinding Content}"/>
        </Grid>
    </ControlTemplate>
    

    and use it like this:

    <Window ... Template="{StaticResource TinyWindowTemplate}">
        <TextBlock Text="this is my content"/>
    </Window>
    

    The ControlTemplate may as well be part of a Window Style resource.