我正在编写一个应用程序,它允许用户更改文本框或标签的属性,这些控件是用户控件。为每个用户控件创建一个单独的类是最简单的,它实现了我希望它们能够更改的属性,然后将它们绑定回用户控件?还是我有另一种方法可以忽略?

有帮助吗?

解决方案

创建自定义属性,并使用此属性标记您希望用户编辑的属性。然后设置 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归因
不隶属于 StackOverflow
scroll top