问题描述
我想在设计时为可浏览的 Windows 窗体创建一个自定义属性,但我的努力都没有成功.显而易见的解决方案似乎是将 browsable 属性设置为 true:
I want to make a custom property for a windows form browsable during design-time but none of my efforts have panned out to success. The obvious solution would seem to be to set the browsable attribute to true:
[Browsable(true),
EditorBrowsable(EditorBrowsableState.Always),
Description("Custom Border Colour"),
Category("Custom")]
public Color BorderColour
{
get
{
return bCol;
}
set
{
bCol = value;
}
}
但这不起作用.我已经为自定义控件做了很多次,它就像一个魅力,事实上,我什至不需要添加属性,因为默认值为 true.这个 codeproject 文章似乎做了我想要,这就是我上面描述的.MSDN也是死路一条,还是不知道要找什么.
But this doesn't work. I have done it numerous time for custom controls and it works like a charm, in fact, I don't even need to add the attributes because the default is true. This codeproject article seems to do what I want, which is what I described above. MSDN is also a dead end, or I don't know what to search for.
我尝试将代码添加到 Form1.cs
和 From1.Designer.cs
但没有任何效果.
I have tried to add the code to Form1.cs
and From1.Designer.cs
but nothing works.
我是否缺少某些东西,例如我需要为表单设置的某些属性以允许这样做,或者这是不可能的?
Is there something I am missing, like some property I need to set for the form to allow for this, or is it just impossible?
我正在使用 Visual Studio Express 2013,如果这会以任何方式影响结果.
I am using Visual Studio Express 2013, if this would influence the outcomes in any way.
Reza 回答后的尝试:这个问题.
推荐答案
简答
您应该将该属性添加到表单的基类中,然后您可以在打开子表单时在设计器中看到它:
You should add the property to base class of your form, then you can see it in designer when you open the child form:
public class Form1 : BaseForm
{
public Form1()
{
InitializeComponent();
}
}
public class BaseForm : Form
{
//The property is not visible in designer of BaseForm
//But you can see it in designer of Form1
public string SomeProperty {get;set;}
}
这种行为背后的原因
原因在于设计师的工作方式.当设计者在设计时显示一个表单时,实际上它创建了一个表单基类的实例并显示了它的属性.因此,在设计器中拥有 public class Form1:Form
,您在设计器中看到的实际上是 Form
类的一个实例,以及使用 using 设置了哪些属性值的控件的实例Form1
的 InitializeComponent
方法以及使用 Form1
的 InitializeComponent
方法添加的控件.
The reason is in the way that designer works. When the designer shows a form at design time, in fact it creates an instance of the base class of the form and shows its properties. So having public class Form1:Form
in designer, what you see in designer is actually an instance of Form
class and the instances of controls which values of properties has been set using using InitializeComponent
method of Form1
and also controls which are added using InitializeComponent
method of Form1
.
同样对于用户控件,您无法在用户控件的设计器中看到您的自定义属性,因为您可以在用户控件的设计器中看到的属性是其基类的属性.但是,当您将用户控件的实例放在表单上时,您会看到该实例的属性,即 UserControl1
的属性.
Also for user controls, you can not see your custom properties in the designer of your user control, because the properties that you can see in designer of your user control, is properties of it's base class. But when you put an instance of your user control on a form, you will see properties of that instance which is properties of your UserControl1
.
设计器的根元素的属性是根元素的基类的属性.但这些值正是 InitializeComponent
中设置的值.
Properties of the root element of your designer are properties of base class of the root element. But the values are exactly those which are set in InitializeComponent
.
要查找更多信息并查看设计师如何工作的有趣示例,您可以查看此帖子或这个.
To find more information and see an interesting example of how the designer works, you can take a look at this post or this one.
这篇关于设计时表单的自定义可浏览属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!