隐藏属性

问题描述:

所以我有一个WPF的DataGrid绑定到一个ObservableCollection,其中包含一类的单个实例 - 例如:隐藏属性

Public Class parent 
    Public Property title As String [...] 
    Public Property someCommonThing as Integer [...] 

Public Class Child Inherits Parent 
    Public Property name As String [...] 
    Public Property address As String [...] 

Public Class Window1 
    Dim oc As ObservableCollection(Of Object) = New ObservableCollection(Of Object) 
    oc.Add(New Child()) 
    dataGrid.ItemsSource = oc 

有很多子类具有不同的特性,因此为什么我无法直接定义datagrid列。我希望能够从数据网格中隐藏某些父属性(例如,从不在数据网格中显示标题属性),同时仍然可以将它用于其他地方的数据绑定(例如标签)。

这可能吗?如果不手动指定每个可能的类的每个列而不使用数据绑定,我无法考虑如何去做。

+0

您是否自动生成列? – Bas 2014-09-02 09:10:32

+0

是的,我是 - 所以我不必告诉它哪个列用于每个不同的类 – simonalexander2005 2014-09-02 09:33:30

当自动生成列,您可以更改使用数据注释的每财产的行为,在这种情况下,特别是BrowsableAttribute类:

<Browsable(False)> 

这个注解你的财产将阻止一列从使用时所产生的在DataGrid的AutoGeneratingColumn事件之后的事件处理程序。

Private Sub OnAutoGeneratingColumn(sender As Object, e As DataGridAutoGeneratingColumnEventArgs) 
    If Not DirectCast(e.PropertyDescriptor, PropertyDescriptor).IsBrowsable Then 
     e.Cancel = True 
    End If 
End Sub 

记得将DataAnnotations程序集添加到您的项目中。

+0

谢谢。这是哪个命名空间? Intellisense似乎无法找到它 – simonalexander2005 2014-09-02 09:33:03

+0

@ simonalexander2005点击我答案中的链接,它位于程序集中名称空间中的'System.ComponentModel.DataAnnotations'中。 – Bas 2014-09-02 09:33:57

+0

啊,这是一个链接!对不起,明白了。谢谢:) – simonalexander2005 2014-09-02 09:40:35