Silverlight:将动态数据绑定到 DataGrid

发布于 2024-09-24 05:16:18 字数 398 浏览 4 评论 0原文

最近遇到一个问题,希望大家帮忙。

我的任务是创建一个可以获取任何数据集的应用程序,并将其显示在一系列网格中(使用选项卡控件之类的东西)。

我可以在 WPF 中轻松完成此操作: 1. 创建一个返回DataSet对象的WCF服务 2. 使用 DataGrid 创建 WPF 窗口 3. 使用 AutoGenerateColumns=True 将 DataSet 的 DataTable 绑定到 WPF DataGrid

现在我被要求在 Silverlight 中执行此操作。所以本质上,我不知道我返回的表会是什么样子,所以我无法创建一个具有可以放入集合和绑定的属性的类。 Silverlight 不允许数据集。

我觉得这对于任何平台来说都是相当普遍的需求,有没有一种方法可以处理这个问题,而不需要大量的编码来解决这些限制?

I've run into a problem recently, hoping you all could help.

I was tasked with creating an application that can take any DataSet, and display it in a series of grids (using something like a tab control).

I was able to do this pretty easily in WPF:
1. Create a WCF Service that returns a DataSet object
2. Create a WPF Window with a DataGrid
3. Bind the DataSet's DataTables to WPF DataGrids using AutoGenerateColumns=True

Now I have been asked to do it in Silverlight instead. So in essence, I will not know what the table will look like that I get back, so I cannot create a class with properties that can be thrown into a collection and bound. Silverlight will not allow DataSets.

I feel as if this should be a fairly common need for any platform, is there a way to handle this that doesn't require a massive amount of coding to workaround the limitations?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

喜你已久 2024-10-01 05:16:18

一种解决方案是将数据集列信息和数据集 XML 传递给 silverlight。在 Silverlight 端,基于那些具有 System.Reflection.Emit 命名空间提供的功能的数据构建动态数据对象。然后将动态构建的DataObject的List绑定到DataGrid上。动态数据对象将为数据集中具有相同数据类型的每一列提供一个属性。

因为除了数据之外我们还有列信息,所以绑定到 DataGrid 可以非常灵活。您可以设置 AutoGenerateColumn = true 以便显示数据集中的所有数据,或者我可以动态生成我想要显示的列。

您可以从以下位置下载示例源代码 这里

One solution is to pass the DataSet Column Information and DataSet XML to the silverlight. On the Silverlight side build a Dynamic Data Object based on those data with functions provided by System.Reflection.Emit namespace. Then bind the List of dynamically build DataObject to the DataGrid. The Dynamic Data Object will have one property for each column in the DataSet with the same DataType.

Because we have the Column Information besides the Data, binding to the DataGrid can be very flexible. you can set AutoGeneratedColumn = true so it display all the Data in the DataSet or I can dynamically generate columns I want to display.

You can download the sample source code from here

开始看清了 2024-10-01 05:16:18

我个人的答案是不使用数据集(无论如何我认为它们非常可怕);相反,让你的 WCF 服务返回业务对象,最好借助一些 ORM 框架(实体框架、NHibernate 等)来使其更容易。

或者,您可以使用 Linq to DataSet 从 DataSet 创建对象,然后再通过 WCF 发送对象。

另请参阅这些 线程 了解更多详细信息。

My personal answer would be to not use DataSets (I think they're quite horrid anyway); instead make your WCF services return business objects, preferably with the help of some ORM framework (Entity Framework, NHibernate etc.) to make it easier.

Or you can use Linq to DataSet to create objects from the DataSet before sending them through WCF.

Also see these threads for more details.

少跟Wǒ拽 2024-10-01 05:16:18

本教程建议了一种方法这看起来比发出动态类简单得多。您基本上利用 Converter 属性绑定到数据行上的索引值。

我刚刚尝试过这个方法,看起来效果很好。这是一些代码:

/// <summary>
/// Allows us to bind columns to dictionary entries.
/// </summary>
public class DictionaryConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var dict = (IDictionary) value;
        return dict[parameter];
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

用法:

List<IDictionary<string, object>> dicts = GetDictionaryData();
foreach (var series in _trendChart.Series)
{
    _dataGrid.Columns.Add(
        new DataGridTextColumn{
            Header = series.Name, 
            Binding = new Binding{
                Converter = new DictionaryConverter(), 
                ConverterParameter = series.Name
            }
        });
}
_dataGrid.ItemsSource = dicts;

更新

本文包含一组更完整的代码,可以添加排序和其他高级功能。

This tutorial suggests an approach that looks a lot simpler than emitting dynamic classes. You basically leverage the Converter property to bind to an indexed value on the row of data.

I just tried this approach, and it seems to work great. Here's some code:

/// <summary>
/// Allows us to bind columns to dictionary entries.
/// </summary>
public class DictionaryConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var dict = (IDictionary) value;
        return dict[parameter];
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

Usage:

List<IDictionary<string, object>> dicts = GetDictionaryData();
foreach (var series in _trendChart.Series)
{
    _dataGrid.Columns.Add(
        new DataGridTextColumn{
            Header = series.Name, 
            Binding = new Binding{
                Converter = new DictionaryConverter(), 
                ConverterParameter = series.Name
            }
        });
}
_dataGrid.ItemsSource = dicts;

Update

This article includes a much more complete set of code that makes it possible to add sorting and other advanced features.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文