【问题标题】:How do I bind to a IEnumerable Dictionary in Silverlight?如何在 Silverlight 中绑定到 IEnumerable 字典?
【发布时间】:2012-01-04 22:56:22
【问题描述】:

背景信息: 我在 WCF 服务中有以下代码。 GetDataTable 根据传递了 query 参数的 SQL 数据库调用返回 System.Data.DataTable。

public IEnumerable<Dictionary<string, object>> GetData(string *query*) {
    var table = GetDataTable(query);
    var columns = table.Columns.Cast<DataColumn>();
    var dict = table.AsEnumerable()
        .Select(r => columns.Select(c => new {
            Column = c.ColumnName,
            Value = r[c]
        })
        .ToDictionary(i => i.Column, i => i.Value != DBNull.Value ? i.Value : null));
    return dict;
}

我有一个 Silverlight 应用程序,它调用 GetData,传递一个字符串,然后我收到结果。但是,我在 GridView 中的字段是“比较器”、“计数”、“键”和“值”。

Silverlight 代码 sn-p

    WCFCLIENT oData = new WCFCLIENT ();
    oData.GetData+= new EventHandler<GetDataCompletedEventArgs>(oData_GetData);
    oData.GetData(*sqlquery*);
  }
}

void oData_GetDataCompleted(object sender, GetDataCompletedEventArgs e) {
  if (e.Error == null) {
    rdpPaging.Source = e.Result;    
    rgvDataResults.ItemsSource = rdpPaging.Source;
}

我的问题有两个

  1. 我用来创建字典的代码是否有错误?
  2. 如果代码正确,如何正确设置 DataGrid 的数据源,使其显示 SQL 调用返回的列和行?

我尝试绑定到 e.Result 变量的不同属性,但结果相似。

【问题讨论】:

  • this 怎么样?
  • @M.Babcock - 项目中的代码似乎很多。它可能有效,但看起来很复杂。

标签: c# silverlight wcf service


【解决方案1】:

你有两个选择...

  1. 直接将字典与 DataGrid 绑定,但保持非自动生成的列。通过遍历总列表的第一项(字典)中的所有键来手动创建列,并使用自定义绑定/转换器来显示正确的数据。

  2. 我将它与 Teleric GridView 一起使用,但我认为这也适用于普通 Silverlight 数据网格。

    http://blogs.telerik.com/blogs/posts/09-04-23/lightweight-datatable-for-your-silverlight-applications.aspx

您可以在客户端将您的列表转换为数据表并轻松使用此方法。

[更新]第一个选项的代码示例

            D_Grid.ItemsSource = Data;        // Data is the collection of dictionary
            foreach (var key in Data[0].Keys)
            {
                    GridViewDataColumn dataCol = null;
                    dataCol = (GridViewDataColumn)D_Grid.Columns[key];
                    if (dataCol == null)
                    {
                        dataCol = new GridViewDataColumn();
                        dataCol.Header = key;
                        dataCol.UniqueName = key;                          
                        dataCol.DataMemberBinding = new Binding()
                        {
                            Converter =new GridConverter(key); // Put your converter that will take the key and return the value from that key.
                        };
                        D_Grid.Columns.Add(dataCol);

                    }
                }

转换器代码。请注意,您需要将密钥存储在您在构造函数中传递的转换器中。

public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value is Dictionary<string, object>)
            {
                var input = (Dictionary<string, object>)value;
                if (input.ContainsKey(_key))
                    return input[_key];
            }   

【讨论】:

  • DataTable 不是 Silverlight 内置的,当我使用 Telerik 时,我的版本似乎没有 DataTable 作为 Telerik.Windows.Data 的一部分。您能否提供您在上面选项 1 中提到的绑定/转换器方法的示例?我可能可以确定如何手动创建列,但对绑定了解不多。
  • 如果您使用第二个选项中的链接,您可以下载 DataTable 的源代码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-02-07
  • 2015-06-26
  • 2014-06-13
  • 1970-01-01
  • 2012-02-09
  • 2010-12-03
  • 2011-06-05
相关资源
最近更新 更多