wpf - UserControl继承 [英] wpf - UserControl inheritance

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

问题描述

我在WPF中遇到控件继承问题。我创建了一个名为BaseUserControl的UserControl。我想让这个控件成为其他WPF userControls的基本控件。所以我写了另一个名为FirstComponent的UserControl。在下一步中,我更改了此代码

I have problem with control inheritance in WPF. I created a UserControl named BaseUserControl. I want for this control to be a base control for other WPF userControls. So I wrote another UserControl called FirstComponent. In next step I changed this code

FirstComponent : UserControl

到此

FirstComponent : BaseControl

但是在编译期间我收到此错误

However during compilation I get this error

Partial declarations of 'controlinheritance.componenets.FirstComponent' must not specify different base classes 

我该怎么办?如何使FirstComponent从BaseControl派生?

What should I do to enable FirstComponent to derive from BaseControl?

编辑
感谢abhishek的回答我设法继承了控件。我有另一个问题。在基类中我指定了一个属性public Grid _MainGrid {get;组; }。现在我想在我的派生类中创建一个这个网格的实例。所以我使用了这段代码



Howerver我收到错误属性'_MainGrid'没有值。第8行位置36.

EDIT Thanks to abhishek answer I managed to inherit controls . Howerver I have another question. In base class I specified a property public Grid _MainGrid { get; set; }. Now I want in my derived class create an instance of this grid. So I used this code Howerver I get an error Property '_MainGrid' does not have a value. Line 8 Position 36.

推荐答案

你看到我的完整文章了吗?

Did you see my complete article on it?

http:// www.dotnetfunda.com/articles/article832-define-base-class-for-window--usercontrol-.aspx

我希望这会对你有所帮助在这。

I hope that would help you in this.


如果您尝试执行该项目,肯定会将错误抛给
你。这是因为,每个WPF窗口都是从baseWindow
布局而不是当前的Window布局创建的。换句话说,如果
看到XAML,你会看到根标签是Window,它是
类,只是当前窗口的父类。

If you try to execute the project, it would definitely throw error to you. This is because, every WPF window is created from the baseWindow layout rather than the current Window layout. In other words, if you see the XAML, you will see the root tag is Window, which is a class just parent of the current window.

因此,为了确保一切正常,我们需要更改Root
元素。

Thus to ensure everything works perfectly, we need to change the Root Element.

所以它看起来像:

<local:BaseWindow Class="BaseWindowSample.Window1" 
                  Name="winImp" 
                  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
                  x="http://schemas.microsoft.com/winfx/2006/xaml" 
                  xmlns:local="clr-namespace:BaseWindowSample" 
                  Title="Window1">
...
</local:BaseWindow>

如果你看到这一点,你可以看到我在
项目中添加了一个命名空间并将其命名为local。所以BaseWindow应该来自
BaseWindow,因此它就像本地一样:BaseWindow

If you see this minutely, you can see I have added one namespace to my project and named it as local. So BaseWindow should come from BaseWindow and thus it goes like local:BaseWindow

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

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