gpt4 book ai didi

WPF - 用户控件继承

转载 作者:行者123 更新时间:2023-12-04 19:20:28 25 4
gpt4 key购买 nike

我在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com