我正在尝试使用System.Windows.Forms.PropertyGrid
.
要使该属性在此网格中不可见,应使用BrowsableAttribute
set to false
.但添加此属性会使属性无法绑定.
例如:创建一个新的Windows窗体项目,拖放TextBox
和PropertyGrid
到Form1
.使用下面的代码,宽度TextBox
不会被绑定到Data.Width
:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Data data = new Data();
data.Text = "qwe";
data.Width = 500;
BindingSource bindingSource = new BindingSource();
bindingSource.Add(data);
textBox1.DataBindings.Add("Text", bindingSource, "Text", true,
DataSourceUpdateMode.OnPropertyChanged);
textBox1.DataBindings.Add("Width", bindingSource, "Width", true,
DataSourceUpdateMode.OnPropertyChanged);
propertyGrid1.SelectedObject = data;
}
}
Run Code Online (Sandbox Code Playgroud)
数据类的代码是:
public class Data : IBindableComponent
{
public event EventHandler TextChanged;
private string _Text;
[Browsable(true)]
public …
Run Code Online (Sandbox Code Playgroud) 我希望隐藏/显示属性取决于用户在drop中做出的选择.我处理事件很好,但实际上不能使正确的属性消失.我在网上发现的解决方案主要是从2005年开始,因为我对它的经验很少,我认为如果隐藏属性很难,我一定做错了.
到目前为止,我已尝试在处理事件后访问该属性,但ReadOnly和IsBrowsable属性是只读的.
propertygrid有一个属性BrowsableAttributes,它接受一个属性列表,但只能负面地工作,不能做 - 或者只做和.提供属性集合; category - 'test'and isbrowsable - true; 返回那些匹配两者的那些,因为我无法提供多个AttributeCollections我不能使搜索具体到足以隐藏必要的搜索,同时让其他人看得见.
在过去的几个小时里,我一直在撞墙,认为必须有一个更简单的方法.