简体   繁体   English

C#属性网格

[英]C# Property Grid

I am writing an application which is going to allows users to change the properties of a text box or label and these controls are user controls. 我正在编写一个应用程序,它将允许用户更改文本框或标签的属性,并且这些控件是用户控件。 Would it be easiest to create a separate class for each user control which implements the properties I want them to be able to change and then bind those back to the user control? 为每个用户控件创建一个单独的类,实现实现我希望它们能够更改的属性,然后将其绑定回用户控件,是否最简单? Or is there another method I am overlooking? 还是我还有其他方法要忽略?

Create a custom Attribute, and tag the properties you want the user to edit with this attribute. 创建一个自定义属性,并使用该属性标记要用户编辑的属性。 Then set the BrowsableAttribute property on the property grid to a collection containing only your custom attribute: 然后将属性网格上的BrowsableAttribute属性设置为仅包含您的自定义属性的集合:

public class MyForm : Form
{
    private PropertyGrid _grid = new PropertyGrid();

    public MyForm()
    {
        this._grid.BrowsableAttributes = new AttributeCollection(new UserEditableAttribute());
        this._grid.SelectedObject = new MyControl();
    }
}

public class UserEditableAttribute : Attribute
{

}

public class MyControl : UserControl
{
    private Label _label = new Label();
    private TextBox _textBox = new TextBox();

    [UserEditable]
    public string Label
    {
        get
        {
            return this._label.Text;
        }
        set
        {
            this._label.Text = value;
        }
    }

    [UserEditable]
    public string Value
    {
        get
        {
            return this._textBox.Text;
        }
        set
        {
            this._textBox.Text = value;
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM