简体   繁体   English

如何在Silverlight中绑定到IEnumerable词典?

[英]How do I bind to a IEnumerable Dictionary in Silverlight?

Background information: I have the following code in a WCF service. 背景信息:我在WCF服务中具有以下代码。 GetDataTable returns a System.Data.DataTable based on a call to a SQL database with the query parameter passed. GetDataTable基于传递了查询参数的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;
}

I have a Silverlight application that makes a call to GetData, passing a string, and I receieve results. 我有一个Silverlight应用程序,该程序调用GetData,传递一个字符串,并且接收结果。 However, the fields I have in my GridView are "Comparer", "Count", "Keys", and "Value". 但是,我在GridView中具有的字段是“比较器”,“计数”,“键”和“值”。

Silverlight code snippet Silverlight代码段

    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;
}

My Question is two-fold 我的问题有两个方面

  1. Is the code I'm using to create the Dictionary wrong somehow? 我用来创建Dictionary的代码不正确吗?
  2. If the code is correct, how do I properly set the DataGrid's data source so it shows the columns and rows returned by the SQL call? 如果代码正确,如何正确设置DataGrid的数据源,使其显示SQL调用返回的列和行?

I have tried binding to different properties of the e.Result variable, but have had similar results. 我尝试绑定到e.Result变量的不同属性,但结果相似。

You have two options ... 您有两个选择...

  1. Bind the dictionary directly with the DataGrid but keep the columns non autogenerated. 将字典直接与DataGrid绑定,但不要自动生成列。 Create the columns manually by loopiing through all the keys in first item (dictionary) of the total list and use custom binding/converter for showing proper data. 通过遍历总列表的第一项(词典)中的所有键来手动创建列,并使用自定义绑定/转换器显示正确的数据。

  2. I am using this with teleric GridView but I think this will work with normal Silverlight datagrid as well. 我将其与TeleGrid GridView一起使用,但我认为这也适用于普通的Silverlight DataGrid。

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

You can convert your list into datatable at clientside and use this approach easily. 您可以在客户端将列表转换为数据表,并轻松使用此方法。

[Update] Code example of the first option [更新]第一个选项的代码示例

            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);

                    }
                }

Converter code. 转换器代码。 Please note you need to store the key in the converter that you pass in the constructor. 请注意,您需要将密钥存储在构造函数中传递的转换器中。

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];
            }   

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM