WPF - 用户控件继承

标签 wpf inheritance user-controls wpf-controls

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

FirstComponent : UserControl

对此
FirstComponent : BaseControl

但是在编译期间我收到此错误
Partial declarations of 'controlinheritance.componenets.FirstComponent' must not specify different base classes 

我应该怎么做才能使 FirstComponent 从 BaseControl 派生?

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



但是我收到一个错误属性“_MainGrid”没有值。第 8 行位置 36。

最佳答案

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

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

我希望这会帮助你。

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.

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

So it would look like :

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

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 - 用户控件继承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3982191/

相关文章:

Java继承概念指的是父类(super class)构造函数吗?

inheritance - Backbone .js 模型继承

C# XAML WPF 在 UserControl 上呈现之前使用参数

WPF 嵌套样式

wpf - 在 Canvas 上定位 UIElement

c# - FlowDocument 内的段落边框显得模糊,SnapsToDevicePixels 和 UseLayoutRounding 均不起作用

c++ - vector 指针,继承

javascript - Youtube JavaScript API - 播放任何一个 youtube 视频时暂停音频

java - 创建android应用程序的多个副本

wpf - InternetGetCookieEx : not working