Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF: Inheriting from Window

I seem to be having some trouble creating a custom window type that derives from System.Windows.Window. There seem to be two problems that are occurring. Firstly, there's a compile-time error stating

Cannot find the static member 'ContentProperty' on the type 'Control'

This is in reference to the ContentPresenter element in the ControlTemplate for the custom window (see code sample for BaseWindowResource.xaml below). I don't know why this is happening, since BaseWindow derives from Window, and therefore must have a Content property...

The second problem is the fact that I can't seem to get the ContentRendered event of BaseWindow to fire when Window1, which derives from BaseWindow, has finished rendering... I need to handle the ContentRendered event in BaseWindow, since the handler will contain a lot of code that would otherwise need to be copied into each derived class...

At any rate, here's the code. Any help will be much appreciated!

Cheers,

Andrew

App.xaml:

<Application x:Class="WpfApplication4.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="Window1.xaml">
    <Application.Resources>
        <ResourceDictionary Source="/BaseWindowResource.xaml" />
    </Application.Resources>
</Application>

BaseWindowResource.xaml:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:local="clr-namespace:WpfApplication4">
    <Style TargetType="{x:Type local:BaseWindow}" x:Key="BaseWindowStyleKey">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate>
                    <Grid>
                        <Rectangle Margin="20" Fill="Green" x:Name="MyRect" />
                        <ContentPresenter Margin="30" x:Name="MyContentPresenter"
                                          Content="{TemplateBinding Content}"
                                          ContentTemplate="{TemplateBinding ContentTemplate}" />
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

BaseWindow.cs:

    public class BaseWindow : Window
    {
        public BaseWindow()
        {
            Style = FindResource("BaseWindowStyleKey") as Style;

            ContentRendered += new EventHandler(BaseWindow_ContentRendered);
        }

        void BaseWindow_ContentRendered(object sender, EventArgs e)
        {
            ContentPresenter contentPresenter = Template.FindName("MyContentPresenter", this) as ContentPresenter;

            MessageBox.Show(String.Format("The dimensions for the content presenter are {0} by {1}",
                contentPresenter.ActualWidth,
                contentPresenter.ActualHeight));
        }
    }

Window1.xaml:

<local:BaseWindow x:Class="WpfApplication4.Window1"
                  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                  xmlns:local="clr-namespace:WpfApplication4"
                  Title="Window1" Height="300" Width="300">
</local:BaseWindow>

and finally Window1.xaml.cs:

public partial class Window1 : BaseWindow
{
    public Window1()
    {
        InitializeComponent();
    }
}

Well, that's all the code. It pretty much isolates the issues.

Cheers,

Andrew

like image 376
Andrew Avatar asked Jan 21 '23 20:01

Andrew


1 Answers

Try specifying the type like following:

Content="{TemplateBinding Window.Content}"

I think the second problem relates to the first one. Post a comment here if second is not solved by this solution.

like image 106
decyclone Avatar answered Feb 01 '23 16:02

decyclone