WPF:从窗口继承 [英] WPF: Inheriting from Window

查看:28
本文介绍了WPF:从窗口继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎在创建派生自 System.Windows.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

找不到静态成员类型上的ContentProperty"'控制'

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

这是对自定义窗口的 ControlTemplate 中的 ContentPresenter 元素的引用(请参阅下面 BaseWindowResource.xaml 的代码示例).我不知道为什么会发生这种情况,因为 BaseWindow 派生自 Window,因此必须具有 Content 属性...

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...

第二个问题是,当派生自 BaseWindow 的 Window1 完成渲染时,我似乎无法触发 BaseWindow 的 ContentRendered 事件...我需要在 BaseWindow 中处理 ContentRendered 事件,因为处理程序将包含大量代码,否则需要将其复制到每个派生类中...

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!

干杯,

安德鲁

应用程序.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:

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:

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:

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>

最后是Window1.xaml.cs:

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.

干杯,

安德鲁

推荐答案

尝试指定如下类型:

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.

这篇关于WPF:从窗口继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆