I have a listbox that has its ItemsSource set to a Dictionary(Of String, ColumnMetadata). ColumnMetadata is a structure. I can easily get the DisplayMemberPath to show the keys by setting it to "Key", but I can’t figure out how to get it to show a member of my structure.
I’ve tried settings DisplayMemberPath to "{Binding LocalizedColumn}", "Value.LocalizedColumn", "LocalizedColumn", "{Value.LocalizedColumn}" and none of those works. I just get a bunch of blank lines in my listbox.
All I’m looking to accomplish is to get the data in to the listbox. I’m not concerned with any updates back to the dictionary and the dictionary won’t be updated after the list is populated.
The code I have now is activated at runtime:
lstDatabaseColumns.ItemsSource = ImportData.GetAddressFieldData
lstDatabaseColumns.DisplayMemberPath = "Value.LocalizedColumn"
lstDatabaseColumns.SelectedValuePath = "Key"
My structure looks like:
Public Structure ColumnMetadata
Dim LocalizedColumn As String
Dim Description As String
End Structure
I’m getting the following message in my output window:
System.Windows.Data Error: 40 : BindingExpression path error: 'LocalizedColumn' property not found on 'object' ''ColumnMetadata' (HashCode=1118531966)'. BindingExpression:Path=Value.LocalizedColumn; DataItem='KeyValuePair`2' (HashCode=-1578744570); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')
I changed my code to a class and Overrode the
ToString()function:Public Class ColumnMetadata Public LocalizedColumn As String Public Description As StringThen I set my runtime assignments to:
lstDatabaseColumns.ItemsSource = ImportData.GetAddressFieldData lstDatabaseColumns.DisplayMemberPath = "Value" lstDatabaseColumns.SelectedValuePath = "Key"Now everything is working. When a user clicks on an item in the list box and they want to do something with the other items in the class, for example assign it to a text box, I’ve implemented this code:
Using that method I can now access any part of the class or the key of the dictionary.
I’m still interested in knowing what to set
DisplayMemberPathto in order to show a specific member of a structure.