您如何/可能/可能将 WPF DataGrid 绑定到每个在字典中具有某些值的对象列表<字符串,字符串>? [英] How can/could/might you bind WPF DataGrid to a List of objects each with some values in a Dictionary&lt;string,string&gt;?

查看:26
本文介绍了您如何/可能/可能将 WPF DataGrid 绑定到每个在字典中具有某些值的对象列表<字符串,字符串>?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

风滚草的主要编辑,可能仍然是风滚草...

major edit of a tumbleweed, may remain a tumbleweed...

如果我有一个客户列表,并且每个客户的数据都包含在字典中,我如何将列表绑定到 DataGrid 以便每个字符串键都是一列?

If I have a list of Customers and the data for each Customer is contained in a Dictionary how can I bind the list to the DataGrid so that each string key is a column?

N.B.我知道这不是设计 Customer 类的好方法.

N.B. I know it's not a nice way to design a Customer class.

例如

public class Customer{

    public int Id{get;set;}

    private Dictionary<string,string> values;

    public Dictionary<string,string> Values{get {return values;}}

    public Customer(int id){
         this.Id = id;
         values["Name"] = "Peter";
         values["Age"] = 129.ToString();
         values["HairColour"] = "See through!";
    }
}

...那天晚些时候...

... later that day...

var Customers = new List<Customer>(){
    new Customer(1),
    new Customer(2), 
    new Customer(3)
};

...然后...

<DataGrid ItemsSource={Binding Path=Customers}/>

...想要的结果.

Id | Name | Age |  HairColour
________________________
1  | Peter| 129 | See through!
________________________

2  | Peter| 129 | See through!
________________________

3  | Peter| 129 | See through!
________________________

推荐答案

在没有其他建议的情况下 这篇文章 是我能想到的最佳答案.在他的文章中,Miron 正在处理来自 Web 服务的 XML 数据,并将其转换为反射生成的类型以进行数据绑定.

In the absence of other suggestions this article is the best answer I can come up with. In his article Miron is working with XML data from a web service and converts it into a Reflection generated type for databinding.

我可以按照他的方法创建一个类型,该类型使用我的 Dictionary 键而不是 xml 节点作为生成类型的属性的来源.对于我正在构建的其他简单应用程序,它看起来相当庞大,但至少我可以了解 Reflection API.

I can follow his approach and create a type that uses my Dictionary keys rather than xml nodes as the source for the properties of the generated type. For the otherwise simple application I'm building it seems rather hefty but at least I get to learn about the Reflection API.

如果有人愿意发表评论或仍然可以为我提供更好的解决方案,我将不胜感激.

If anyone cares to comment or can still provide me with a better solution I'd appreciate it.

或者,简单的道路......

Or, the easy road...

public partial class Window2 : Window
{
    public Window2()
    {
        InitializeComponent();

        var a1 = new A();
        a1["Name"] = "Jack";
        a1["Age"] = "9";

        var a2 = new A();
        a2["Name"] = "Jill";
        a2["Age"] = "7";

        List<A> items = new List<A>() { a1, a2 };

        this.DataBoundItems = items;

        dg.DataContext = this;
    }

    public List<A> DataBoundItems { get; set; }

    private void dg_DataContextChanged(
        object sender, 
        DependencyPropertyChangedEventArgs e)
    {
        foreach (string key in DataBoundItems[0].Values.Keys)
        {
            var col = new DataGridTextColumn();
            col.Header = key;
            //  bind to the indexer on the class
            col.Binding = new Binding("[" + key + "]");
            dg.Columns.Add(col);
        }
    }
}

public class A
{
    private Dictionary<string, string> values = new Dictionary<string, string>();

    public string this[string index]
    {
        get
        {
            return values[index];
        }
        set
        {
            values[index] = value;
        }
    }

    public Dictionary<string, string> Values { get { return aValues; } }
}

这篇关于您如何/可能/可能将 WPF DataGrid 绑定到每个在字典中具有某些值的对象列表<字符串,字符串>?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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