我正在编写一个应用程序,它允许用户更改文本框或标签的属性,这些控件是用户控件。为每个用户控件创建一个单独的类来实现我希望他们能够更改的属性然后将它们绑定回用户控件是最简单的吗?还是我忽略了另一种方法?
问问题
1153 次
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 回答