ObservableCollection返回错误的类型

时间:2016-08-05 15:29:06

标签: wpf vb.net

出于某种原因,我将findall函数中的每个项目添加到observable集合中,我在列表框中找回“groupname”而不是该组的实际名称。

    Dim ctx As New PrincipalContext(ContextType.Domain)
    Dim qbeGroup As New GroupPrincipal(ctx)
    Dim srch As New PrincipalSearcher(qbeGroup)
    For Each item In srch.FindAll()
        GroupsList.Add(New groups With { _
          .name = item.Name
          })
    Next

以下是我简单的observablecollection

    Public Property name() As String
        Get
            Return m_name
        End Get
        Set(value As String)
            m_name = value
        End Set
    End Property
    Private m_name As String

我还需要做些什么才能返回实际的字符串吗?

编辑:Xaml

  <ListBox x:Name="lbAvGroups_Copy" HorizontalAlignment="Left" Height="470" VerticalAlignment="Top" Width="355" Margin="392,35,0,0"/>

我在代码隐藏中进行绑定,所以这就是我绑定的样子。

 Public View As ICollectionView
 Public GroupsList As new ObservableCollection(Of groups)

 View = CollectionViewSource.GetDefaultView(GroupsList)
 lbAvGroups.ItemsSource = View

1 个答案:

答案 0 :(得分:1)

ListBox绑定到对象列表时,默认情况下会显示通过在对象上调用ToString返回的值。您可以为ItemTemplate定义ListBox,也可以为简单文本显示将DisplayMemberPath设置为您要显示的媒体资源的名称。因此,在您的情况下,您可以添加

lbAvGroups.DisplayMemberPath = "name"

绑定列表框以显示每个项目的name属性。

相关问题