从可观察的收藏中隐藏属性 [英] Hide a property from an observablecollection

查看:118
本文介绍了从可观察的收藏中隐藏属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个WPF DataGrid绑定到一个ObservableCollection,它包含一个类的一个实例,例如:

So I have a WPF DataGrid bound to an ObservableCollection, which contains a single instance of a class - for example:

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列。

there are many child classes with different properties, hence why I can't easily define the datagrid columns directly.

我想要隐藏某些父属性从datagrid(例如,不要在datagrid中显示标题属性),同时仍然可以将其用于其他地方的数据绑定(例如标签)。

I want to be able to hide certain parent properties from the datagrid (for example, never show the title property in the datagrid), while still being able to use it for databinding elsewhere (e.g. a label).

是这可能吗我不能想像如何做,而不用手动指定每个可能的类的每列,而不是使用数据绑定。

Is this possible? I can't think how to do it without manually specifying every column for every possible class instead of using the databinding.

推荐答案

自动生成列可以使用数据注释更改每个属性行为,在这种情况下,特别是 BrowsableAttribute a> class:

When automatically generating columns you can change the per-property behavior using Data Annotations, in this case specifically the BrowsableAttribute class:

<Browsable(False)>

使用此注释您的属性将阻止在AutoGeneratingColumn上使用以下事件处理程序时生成列事件的DataGrid。

Annotating your property with this will prevent a column from being generated when using the following event handler on the AutoGeneratingColumn event of the DataGrid.

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程序集添加到项目中。

Remember to add the DataAnnotations assembly to your project.

这篇关于从可观察的收藏中隐藏属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆