C#プロパティグリッド
-
10-07-2019 - |
質問
ユーザーがテキストボックスまたはラベルのプロパティを変更できるようにするアプリケーションを作成しています。これらのコントロールはユーザーコントロールです。プロパティを実装するユーザーコントロールごとに個別のクラスを作成し、それらを変更してからユーザーコントロールにバインドするのが最も簡単ですか?または私が見落としている別の方法がありますか?
解決
カスタム属性を作成し、この属性を使用してユーザーが編集するプロパティにタグを付けます。次に、 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;
}
}
}
所属していません StackOverflow