继承UserControls问题 [英] Inherited UserControls issues

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

问题描述

我最近遇到了一个棘手的情况:



1)


I有一个UserControl(BasePanel)在它的构造函数中设置一些属性(其中一个是Dock)


然后我必须专门化这个控件。我创建了一个从BasePanel派生的新继承UserControl(OptionPanel)。问题是,每次我在设计器中打开它时,设计器都会重写I​​nitializeComponent()方法(即使它的实际内容甚至没有改变)。


这甚至是陌生人,因为BasePanel控件没有这样做。



2)


我试过找到一个变通方法让一个继承的用户控件来处理它的属性中的默认值:我试图覆盖Dock属性并使用相关属性设置它的默认值。


设置一个新属性(如Dock2)可以工作并显示在属性网格中,覆盖的Dock不会显示,也没有所需的效果(使用默认值)代码是相同的(除了override关键字 - 我甚至尝试过新的关键字 - 以及属性的名称)



是否已遇到这些问题?这是一个错误还是我做错了什么?


有人会想到的任何解决方法吗?



-Fabio

解决方案

您应该尝试添加以下私有方法,以确定Dock属性是否已序列化。
< br> private bool ShouldSerializeDock()
{
返回Dock!= DockStyle.Left;
}

显然将测试值更改为您应该的默认值。

Phil Wright
.NET Windows的免费用户控件.Form
http ://www.componentfactory.com


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 Wright
Free user controls for .NET Windows.Form
http://www.componentfactory.com


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

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