Windows runtime 将字典绑定到WinRT列表框

Windows runtime 将字典绑定到WinRT列表框,windows-runtime,Windows Runtime,我已经读过很多关于将字典绑定到WPF ListView和ListBox的文章,但我无法获得在WinRT中工作的等效代码 <Grid Margin="10" Width="1000" VerticalAlignment="Stretch"> <ListBox Name="StatListView" ItemsSource="{Binding FooDictionary}" > <ListBox.ItemTemplate>

我已经读过很多关于将字典绑定到WPF ListView和ListBox的文章,但我无法获得在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;
        }
    }

公共词典
{
得到
{
Dictionary temp=新字典();
临时添加(“键1”、“值1”);
临时添加(“键2”、“值2”);
临时添加(“键3”、“值3”);
临时添加(“键4”、“值4”);
返回温度;
}
}

什么是正确的绑定?

输出窗口中的错误是(修剪到最有用的部分):

在内部,WinRT正在将类型转换为:

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

另一方面,至少从一个简单的测试来看,根本不是字典引起了问题,而是一个
keyvaluepairimpl
对象实例被转换成上述
CLRIKeyValuePairImpl
类型。我尝试使用一个列表并向列表中添加一个
KeyValuePair
实例,但也失败了。

我想出了一个解决方法,它涉及到动态生成您自己的键值对

如果您有专门的词典,只需添加以下内容:

    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;
            }
         }
    }
公共IEnumerable集合
{
得到{
foreach(本文件中的var v)
{
MyPair p=newmypair(){Key=v.Key,Value=v.Value};
收益率p;
}
}
}
并定义您的配对类型:

public class MyPair<K, V>
{
    public K Key { get; set; }
    public V Value { get; set; }
}
公共类MyPair
{
公钥{get;set;}
公共V值{get;set;}
}

另外,每次创建新对象时要小心。如果您尝试像我最初那样重复使用MyPair,某些项目会穿过对象并存储退货,这会导致所有项目看起来都像最后一个项目。

谢谢!我没有想到在运行时会有如此明显的错误。我很感激你的快速反应。我也很惊讶!不过我承认,在这之前,我不需要在WPF、Silverlight或XAML中将KVP直接添加到UI绑定中,这不仅仅是为了测试。幸运的是,这是一个简单的解决办法。:)
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; }
}
    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;
            }
         }
    }
public class MyPair<K, V>
{
    public K Key { get; set; }
    public V Value { get; set; }
}