字典绑定到WPF的ListView [英] Dictionary Binding to ListView WPF

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

问题描述

我有一个包含字典项的列表。这意味着

列表[0] =字典项=>第[0] = ID,第[1] =名称,项目[2] =金额。

我需要在一个网格的方式ListView控件来显示这一点。这本词典可以有所不同。

更新:

在列表中每个项目看起来是这样的:

[_ ID] =11212131
[标题] =这是标题
[dateCreated会] =一些最新

字典内的项目可以是不同的。

更新2:

我用下面的code创建一个动态的GridView控件,然后添加到列GridView控件。它的工作原理,但现在有相同的周反复列的长水平线。我需要显示的列名,并根据该属于该列中的数据。

  VAR的GridView =新的GridView();            的foreach(在OBJ文件变种O)
            {
                VAR DIC = O作为字典<弦乐,对象&gt ;;
                VAR枚举= dic.GetEnumerator();
                而(enumerator.MoveNext())
                {
                    VAR电流= enumerator.Current;
                    VAR gridViewColumn =新GridViewColumn();
                    gridViewColumn.Header = current.Key;
                    VAR约束力=新绑定(current.Key);
                    //binding.Source =电流;                    gridViewColumn.DisplayMemberBinding =绑定;                    gridview.Columns.Add(gridViewColumn);                }                //新行            }            lvCollections.View = gridview的;

更新3:

我是pretty接近。它的工作原理,但只显示一个长单排反复列。

  VAR的GridView =新的GridView();
            的foreach(在OBJ文件变种O)
            {                VAR DIC = O作为字典<弦乐,对象&gt ;;
                VAR枚举= dic.GetEnumerator();
                而(enumerator.MoveNext())
                {VAR gridViewColumn =新GridViewColumn();
                    VAR电流= enumerator.Current;
                    gridViewColumn.Header = current.Key;
                    VAR约束力=新绑定();
                    binding.Source =电流;
                    binding.Path =新的PropertyPath(值);                    gridViewColumn.DisplayMemberBinding =绑定;
                    gridview.Columns.Add(gridViewColumn);                }
                //新行            }            lvCollections.ItemsSource = OBJ文件;
            lvCollections.View = gridview的;


解决方案

生成你的GridView列如下:

 公共无效OnDataContextChanged(...)
{
  VAR distinctKeys =(
    从字典中(列表与LT;字典<字符串对象>>)的DataContext
    从dict.Keys关键
    选择键
  )。不同();  gridView.Columns.ReplaceWith(
    从distinctKeys关键
    关键的OrderBy
    选择新GridViewColumn
    {
      标题=键,
      DisplayMemberBinding =新的绑定([+键+]),
    });
}//以上code使用像这样的扩展方法
静态无效ReplaceWith< T>(这ObserableCollection< T>收集,IEnumerable的< T> newItems)
{
  collection.Clear();
  的foreach(在newItems VAR项)
    collection.Add(项目);
}

这会工作,除非你的词典键包含特殊字符。在进行此工作的关键是在绑定路径indxer语法来(在'['和']')

I have a List which contains Dictionary items. This means

List[0] = Dictionary Item => Item[0] = id, Item[1] = Name, Item[2] = Amount.

I need to show this in a ListView control in a Grid manner. The dictionary can vary.

UPDATE:

Each Item in the List looks like this:

["_id"] = "11212131" ["Title"] = "this is title" ["DateCreated"] = "some date"

The items inside the dictionary can be different.

UPDATE 2:

I am using the following code to create a dynamic Gridview control and then add the columns into the GridView control. It works but now there is a long horizontal line of same repeative columns. I need to display the column name and under that the data that belongs to that column.

  var gridview = new GridView();

            foreach (var o in objs)
            {
                var dic = o as Dictionary<String, Object>;
                var enumerator = dic.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    var current = enumerator.Current;
                    var gridViewColumn = new GridViewColumn();
                    gridViewColumn.Header = current.Key; 
                    var binding = new Binding(current.Key);
                    //binding.Source = current;

                    gridViewColumn.DisplayMemberBinding = binding;

                    gridview.Columns.Add(gridViewColumn);

                }

                // new row 

            }

            lvCollections.View = gridview; 

UPDATE 3:

I am pretty close. It works but it displays only a long single row with repeated columns.

 var gridview = new GridView();


            foreach (var o in objs)
            {

                var dic = o as Dictionary<String, Object>;
                var enumerator = dic.GetEnumerator();
                while (enumerator.MoveNext())
                {var gridViewColumn = new GridViewColumn(); 
                    var current = enumerator.Current;
                    gridViewColumn.Header = current.Key;
                    var binding = new Binding();
                    binding.Source = current; 
                    binding.Path = new PropertyPath("Value"); 

                    gridViewColumn.DisplayMemberBinding = binding;
                    gridview.Columns.Add(gridViewColumn);

                }


                // new row 

            }

            lvCollections.ItemsSource = objs; 
            lvCollections.View = gridview; 

解决方案

Generate your GridView columns like this:

public void OnDataContextChanged(...)
{
  var distinctKeys = (
    from dict in (List<Dictionary<string,object>>)DataContext
    from key in dict.Keys
    select key
  ).Distinct();

  gridView.Columns.ReplaceWith(
    from key in distinctKeys
    orderby key
    select new GridViewColumn
    {
      Header = key,
      DisplayMemberBinding = new Binding("[" + key + "]"),
    });
}

// The above code uses an extension method like this
static void ReplaceWith<T>(this ObserableCollection<T> collection, IEnumerable<T> newItems)
{
  collection.Clear();
  foreach(var item in newItems)
    collection.Add(item);
}

This will work unless your dictionary key contains special characters. The key to making this work is the indxer sytax in the binding path (the '[' and ']').

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

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