设计时 Form 的自定义可浏览属性 [英] Custom browsable property for Form at design time

查看:25
本文介绍了设计时 Form 的自定义可浏览属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在设计时为可浏览的 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.csFrom1.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 设置属性值的控件实例Form1InitializeComponent 方法以及使用Form1InitializeComponent 方法添加的控件.

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.

这篇关于设计时 Form 的自定义可浏览属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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