0

ユーザーがテキストボックスまたはラベルのプロパティを変更できるようにするアプリケーションを作成しています。これらのコントロールはユーザーコントロールです。ユーザーコントロールごとに個別のクラスを作成して、変更できるようにしたいプロパティを実装してから、それらをユーザーコントロールにバインドするのが最も簡単でしょうか。または私が見落としている別の方法はありますか?

4

1 に答える 1

1

カスタム属性を作成し、この属性でユーザーが編集できるようにするプロパティにタグを付けます。次に、プロパティ グリッドの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;
        }
    }
}
于 2009-10-29T15:10:01.940 に答える