WPF: Inheriting from Window

六眼飞鱼酱① 提交于 2019-12-23 12:03:22

问题


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


回答1:


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.




回答2:


adding TargetType should solve the problem

exemple

 <ControlTemplate TargetType="Window">


来源:https://stackoverflow.com/questions/4572304/wpf-inheriting-from-window

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!