我最近遇到了一个棘手的情况:
1)
I有一个UserControl(BasePanel)在它的构造函数中设置一些属性(其中一个是Dock)
然后我必须专门化这个控件。我创建了一个从BasePanel派生的新继承UserControl(OptionPanel)。问题是,每次我在设计器中打开它时,设计器都会重写InitializeComponent()方法(即使它的实际内容甚至没有改变)。
这甚至是陌生人,因为BasePanel控件没有这样做。
2)
我试过找到一个变通方法让一个继承的用户控件来处理它的属性中的默认值:我试图覆盖Dock属性并使用相关属性设置它的默认值。
设置一个新属性(如Dock2)可以工作并显示在属性网格中,覆盖的Dock不会显示,也没有所需的效果(使用默认值)代码是相同的(除了override关键字 - 我甚至尝试过新的关键字 - 以及属性的名称)
是否已遇到这些问题?这是一个错误还是我做错了什么?
有人会想到的任何解决方法吗?
-Fabio
解决方案
您应该尝试添加以下私有方法,以确定Dock属性是否已序列化。 private bool ShouldSerializeDock() {返回Dock!= DockStyle.Left; } 显然将测试值更改为您应该的默认值。 Phil Wright .NET Windows的免费用户控件.Form http ://wwwponentfactory
I Recently ran into a tricky situation :
1)
I had a UserControl (BasePanel) setting a few properties in it's constructor ( one of which was Dock )
I then had to specialize this control. I made a new inherited UserControl (OptionPanel) deriving from BasePanel. the problem is that everytime I opened it in the designer, the InitializeComponent() method would be rewritten by the designer ( even if it's actual content didn't even change ).
This is even stranger, as the BasePanel control didn't behave this way.
2)
I tried to find a workaround to have an inherited user control to work with default values in it's properties : I tried to override the Dock property and setting it's default value with the related attribute.
While setting a new Property ( such as Dock2 ) would work and be shown in the property grid, the overriden Dock wouldn't show, nor have the desired effect (use the default value) The code was the same ( except the override keyword -I even tried the new keyword- and the name of the property)
Did already ran into these problems ? Is it a bug or am I doing something wrong ?
any workaround someone would think of ?
-Fabio
解决方案You should try adding the following private method in order to decide if the Dock property is serialized.private bool ShouldSerializeDock(){ return Dock != DockStyle.Left;}Obviously change the tested value to whatever your default value should be.Phil WrightFree user controls for .NET Windows.Formwwwponentfactory
更多推荐
继承UserControls问题
发布评论