Setting DisplayMemberPath to Membership in a Dictionary Structure

I have a list that has it ItemsSourceinstalled. ColumnMetadata- structure. I can easily get DisplayMemberPathto show the keys by setting it to "Key", but I can’t figure out how to make it show a member of my structure.

I tried setting DisplayMemberPathup "{Binding LocalizedColumn}", "Value.LocalizedColumn", "LocalizedColumn", "{Value.LocalizedColumn}", and none of them work. I just get a bunch of blank lines in my list.

All I want to do is get the data in the list. I am not interested in any updates back to the dictionary, and the dictionary will not be updated after filling out the list.

The code that I now activated at runtime:


        lstDatabaseColumns.ItemsSource = ImportData.GetAddressFieldData
        lstDatabaseColumns.DisplayMemberPath = "Value.LocalizedColumn"
        lstDatabaseColumns.SelectedValuePath = "Key"

My structure looks like this:


    Public Structure ColumnMetadata
        Dim LocalizedColumn As String
        Dim Description As String
    End Structure

The following message appears 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')
+3
2

ToString():


    Public Class ColumnMetadata
        Public LocalizedColumn As String
        Public Description As String
    Public Overrides Function ToString() As String
        Return LocalizedColumn
    End Function
End Class

>

:


        lstDatabaseColumns.ItemsSource = ImportData.GetAddressFieldData
        lstDatabaseColumns.DisplayMemberPath = "Value"
        lstDatabaseColumns.SelectedValuePath = "Key"

. , - , , , :


lblColumnDescription.Text = DirectCast(lstDatabaseColumns.SelectedItem, KeyValuePair(Of String, ImportData.ColumnMetadata)).Value.Description

, .

, DisplayMemberPath, .

0

DisplayMemberPath="Value.MyValue", .

:

public class MyClass
{
    public string MyValue { get; set; }
}

public Dictionary<int, MyClass> Data
 {
     get
     {
         Dictionary<int, MyClass> data = new Dictionary<int, MyClass>();
         data[0] = new MyClass { MyValue = "A" };
         data[1] = new MyClass { MyValue = "B" };

         return data;
     }
 }

Xaml:

 <ListBox DisplayMemberPath="Value.MyValue" x:Name="lst" ItemsSource="{Binding ElementName=local, Path=Data}" />

!

+2

Source: https://habr.com/ru/post/1773962/


All Articles