所以我有一个绑定到ObservableCollection的WPF DataGrid,它包含一个类的单个实例 - 例如:
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列。
我希望能够从数据网格中隐藏某些父属性(例如,永远不会在数据网格中显示title属性),同时仍然可以将其用于其他地方的数据绑定(例如标签)。
这可能吗?如果不为每个可能的类手动指定每个列而不使用数据绑定,我无法思考如何操作。
答案 0 :(得分:1)
自动生成列时,您可以使用数据注释更改每个属性的行为,在本例中特别是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程序集添加到项目中。