即使browsable属性设置为false,WPF工具包DataGrid也会显示字段

Jon*_*han 9 c# wpf attributes browsable

您有一个可观察的集合,我使用DataGrid的itemsource属性绑定到DataGrid.

集合中类的所有属性都在DataGrid中正确显示.现在,我想使用类中的browsable属性[Browsable(false)]将一些字段隐藏到DataGrid.它在winforms中运行良好,但似乎在WPF中不起作用.

有人知道为什么吗?我可以稍后隐藏列,但我不想以这种方式丢失性能.

还有其他解决方案吗?

谢谢.

小智 10

此代码适用于.NET 4.0

void m_grid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
    if (((PropertyDescriptor)e.PropertyDescriptor).IsBrowsable == false)
       e.Cancel = true;
}
Run Code Online (Sandbox Code Playgroud)


小智 1

如果能够设置成员属性并让 .NET 4.0 DataGrid 尊重它们,那就太好了。