将字典绑定到 WinRT 列表框 [英] Binding a Dictionary to a WinRT ListBox

查看:28
本文介绍了将字典绑定到 WinRT 列表框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了许多关于将字典绑定到 WPF ListView 和 ListBox 的帖子,但我无法获得在 WinRT 中工作的等效代码.

I've read a number of posts on binding Dictionary to WPF ListView and ListBox but I can't get 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;
        }
    }

什么是正确的绑定?

推荐答案

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

The error in the output window is (trimmed 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, ....

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

Internally, WinRT is converting the type to:

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

如果您添加到您的数据模板:

If you add to your DataTemplate:

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

您会看到它使用 String, String 发出上述类型.

You'll see that it emits the above type with String, String.

但是,由于某种原因,它没有按预期正确处理.如果您在 Internet 上搜索该类型,您会看到有一个 已记录错误,用于解决 Connect 上的问题.

However, for some reason, it's not being properly handled as expected. If you search for that type on the Internet, you'll see that there's a documented bug for the issue on Connect.

一个简单的解决方法是将您的数据放在一个不是 KeyValuePair 的简单对象中:

A simple work around would be to place your data in a simple object that is not a 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; }
}

顺便说一句,至少从一个简单的测试来看,根本不是引起问题的字典,而是将 KeyValuePair 对象实例转换为 CLRIKeyValuePairImpl 的事实 上面提到的类型.我尝试只使用列表并将 KeyValuePair 实例添加到列表中,但也失败了.

As an aside, from a simple test at least, it's not the Dictionary that's causing the issue at all, it's the fact that it's a KeyValuePair object instance that's being converted to the CLRIKeyValuePairImpl type mentioned above. I tried just using a list and adding a KeyValuePair<string, string> instance to a List, and that failed as well.

这篇关于将字典绑定到 WinRT 列表框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆