WPF绑定列表<字典< string,string>>到一个ListBox [英] WPF binding List<Dictionary<string,string>> to a ListBox

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

问题描述

  var list = new List< Dictionary< string,string>> {
new Dictionary< string,string> {
{id,1 },
{name,foo},
},
新词典< string,string> {
{id,2},
{name,bar},
}
};

我想将此列表绑定到列表框。这很简单:

  listBox.ItemsSource = list; 

但问题是:我无法控制列表框中显示的内容。我想要显示的是dict [name]。我试过:

  listbox.DisplayMemberPath =name
解决方案

您的代码尝试显示一个名称名称的属性,不存在。要访问索引器,请使用以下语法:

  listBox.DisplayMemberPath =[name]; 

此外,您应该可以直接在XAML中设置这样的东西,而不是代码隐藏。 / p>

var list = new List<Dictionary<string, string>>{
                new Dictionary<string,string>{
                    {"id","1"},
                    {"name","foo"},
                }, 
                new Dictionary<string,string>{
                    {"id","2"},
                    {"name","bar"},
                }
            };

I want to bind this list to a listbox. It's quite simple:

listBox.ItemsSource=list;

but the problem is: I can't control what is displayed in the listbox. What I want is to display is dict["name"]. I tried:

listbox.DisplayMemberPath="name"

Sadly it doesn't work.

解决方案

Your code tries to display a property called name on the dictionary, which doesn't exist. To access an indexer use the following syntax:

listBox.DisplayMemberPath = "[name]";

Also, you should be probably setting things like this directly in XAML, not in code-behind.

这篇关于WPF绑定列表&lt;字典&lt; string,string&gt;&gt;到一个ListBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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