TonyW
TonyW

Reputation: 786

ObservableCollection returning wrong type

For some reason I'm adding each item in the findall function to the observable collection, and I am getting back "groupname" in my listbox.. instead of the actual name of the group.

    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

Below is my simple 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

Is there something else I need to be doing to return the actual string?

Edit: Xaml

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

I do the binding in code-behind, so this is what my binding looks like.

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

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

Upvotes: 0

Views: 50

Answers (1)

Mark
Mark

Reputation: 8160

When you bind a ListBox to a list of objects it will, by default, display the value returned by calling ToString on the objects. You can either define the ItemTemplate for the ListBox, or for simple text display you can set the DisplayMemberPath to the name of the property you want to show. So, in your case you can add

lbAvGroups.DisplayMemberPath = "name"

when binding the list box to display the name property of each item.

Upvotes: 1

Related Questions