Associating a Dictionary with WinRT ListBox

I read several posts about binding a dictionary to WPF ListView and ListBox, but I can’t get the equivalent code to work in WinRT.

<Grid Margin="10" Width="1000" VerticalAlignment="Stretch"> <ListBox Name="StatListView" ItemsSource="{Binding FooDictionary}" > <ListBox.ItemTemplate> <DataTemplate > <Grid Margin="6"> <StackPanel Orientation="Horizontal" > <TextBlock Text="{Binding Key}" Margin="5" /> <TextBlock Text="{Binding Value}" Margin="5" /> </StackPanel> </Grid> </DataTemplate> </ListBox.ItemTemplate> </ListBox> </Grid> public Dictionary<string, string> FooDictionary { get { Dictionary<string, string> temp = new Dictionary<string, string>(); temp.Add("key1", "value1"); temp.Add("key2", "value2"); temp.Add("key3", "value3"); temp.Add("key4", "value4"); return temp; } } 

What is the correct binding?

+4
source share
2 answers

Error in the output window (cropped to the most useful part):

 Error: Cannot get 'Key' value (type 'String') from type 'System.Runtime.InteropServices.WindowsRuntime.CLRIKeyValuePairImpl`2 [[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], mscorlib, .... 

Inside WinRT converts the type to:

 System.Runtime.InteropServices.WindowsRuntime.CLRIKeyValuePairImpl<K, V> 

If you add to your DataTemplate:

 <TextBlock Text="{Binding}" Margin="5" /> 

You will see that he selected the above type with String, String .

However, for some reason, it is not being processed properly as expected. If you search for this type on the Internet, you will see that there was a documentary error for the problem in Connect.

A simple job should be to place your data in a simple object that is not KeyValuePair:

 List<StringKeyValue> temp = new List<StringKeyValue>(); temp.Add(new StringKeyValue { Key = "key1", Value = "value1" } ); temp.Add(new StringKeyValue { Key = "key2", Value = "value2" }); temp.Add(new StringKeyValue { Key = "key3", Value = "value3" }); temp.Add(new StringKeyValue { Key = "key4", Value = "value4" }); this.DefaultViewModel["FooDictionary"] = temp; public class StringKeyValue { public string Key { get; set; } public string Value { get; set; } } 

In any case, from a simple test, at least this is not the dictionary that causes the problem at all, it is the fact that the KeyValuePair object KeyValuePair that was converted to the CLRIKeyValuePairImpl type mentioned above. I tried to just use the list and add an instance of KeyValuePair<string, string> to the list, and that also failed.

+7
source

I came up with a workaround that involves dynamically creating my own key pairs.

If you have a specialized dictionary, just add this:

  public IEnumerable<MyPair<K, V>> Collection { get { foreach (var v in this) { MyPair<K, V> p = new MyPair<K, V>() { Key = v.Key, Value = v.Value }; yield return p; } } } 

and indicate your type of pair:

 public class MyPair<K, V> { public K Key { get; set; } public V Value { get; set; } } 

Also, be careful to create a new object each time. Some elements move around the object and keep returning, which can lead to everything that looks like the last element if you try to reuse MyPair, as I originally did.

0
source

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


All Articles