问题描述
我有这样的课:
private class MyClass {
[DisplayName("Foo/Bar")]
public string FooBar { get; private set; }
public string Baz { get; private set; }
public bool Enabled;
}
当我创建 List
并将其分配给 DataGridView
的 DataSource
时,网格会显示两列,"Foo/Bar"和Baz".这就是我想要发生的事情.
When I create a List<MyClass>
and assign it to the DataSource
of a DataGridView
, the grid shows me two columns, "Foo/Bar" and "Baz". This is what I want to happen.
它目前有效,因为 Enabled 是一个字段,而不是一个属性 - DataGridView 只会获取属性.然而,这是一个肮脏的黑客.
It currently works because Enabled is a field, not a property - DataGridView will only pick up properties. However, this is a dirty hack.
我也想将 Enabled 设为属性,但仍将其隐藏在 DataGridView 上.
I would like to make Enabled a property too, but still hide it on the DataGridView.
我知道我可以在绑定后手动删除该列..但这并不理想.
I know I can manually delete the column after binding.. but that's not ideal.
是否有类似于 DisplayName 的属性,我可以用它来标记属性?[Visible(false)]
之类的东西?
Is there an attribute similar to DisplayName, that I can tag a property with? Something like [Visible(false)]
?
推荐答案
[Browsable(false)]
将隐藏 DataGridView
的属性.
视觉设计器通常会在属性"窗口中显示那些没有可浏览属性或标有BrowsableAttribute 构造函数的可浏览参数设置为 true.这些成员可以在设计时进行修改.标有BrowsableAttribute 构造函数的可浏览参数设置为 false 是不适合设计时编辑,因此不适合显示在视觉设计器中.默认为真.
A visual designer typically displays in the Properties window those members that either have no browsable attribute or are marked with the BrowsableAttribute constructor's browsable parameter set to true. These members can be modified at design time. Members marked with the BrowsableAttribute constructor's browsable parameter set to false are not appropriate for design-time editing and therefore are not displayed in a visual designer. The default is true.
这篇关于是否有一个属性我可以在我的类中使用来告诉 DataGridView 在绑定到列表时不要为其创建列<MyClass>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!