假设我有一个我想要在 DataGridView 中显示的属性,但当同一对象显示在 PropertyGrid 中时却没有。我知道我可以使用 [Browsable(false)]
,但这将它隐藏在两个 View 中。我也可以执行 gridView.Columns["blah"].Visible = false;
,但这与我想要的相反,因为它隐藏在 DataGridView 而不是 PropertyGrid 中。有什么办法可以反过来吗? (除了创建一个全新的 DataTable 只是为了保存相同的数据减去一个字段,而是将所有内容重新绑定(bind)到该字段 - 这确实是一种做事的方式。)或者,我可以使用向 DataGridView 添加一列的解决方案实际类(class)中不存在。
最佳答案
可以通过使用 PropertyGrid 的 BrowsableAttributes 属性来解决这个问题。 首先,像这样创建一个新属性:
public class PropertyGridBrowsableAttribute : Attribute
{
private bool browsable;
public PropertyGridBrowsableAttribute(bool browsable){
this.browsable = browsable;
}
}
然后将此属性添加到您希望在 PropertyGrid 中显示的所有属性:
[DisplayName("First Name"), Category("Names"), PropertyGridBrowsable(true)]
public string FirstName {
get { return ... }
set { ... }
}
然后像这样设置 BrowsableAttributes 属性:
myPropertyGrid.BrowsableAttributes = new AttributeCollection(
new Attribute[] { new PropertyGridBrowsableAttribute(true) });
这只会在您的属性网格中显示属性属性,DataGridView 仍然可以访问所有属性,只需多做一点编码工作。
我仍然会选择 Tergiver 并将此行为称为错误,因为 Browsable 属性的文档清楚地说明了它仅用于属性窗口。
(归功于 http://www.mycsharp.de/wbb2/thread.php?postid=234565 的用户“maro”)
关于c# - 使属性在 DataGridView 中可见但在 PropertyGrid 中不可见?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10726543/