是否有 ITypedList 的 Silverlight 等效项?

发布于 2024-12-06 06:45:22 字数 761 浏览 0 评论 0原文

我有一个对象集合,每个对象都包含一组名称/值对。所有对象的名称都是相同的。我想将它们显示为数据网格中的列。

在 Winforms/WPF 中,我将使用 ITypedList 和一些 PropertyDescriptor 实例来向运行时提供一些假属性。然而这种类型在 Silverlight 中似乎不可用。

那么,是否有替代方案,或者 Silverlight 中不存在这种替代方案?

编辑添加一些代码以更好地构建场景

public class Cell {
    public string Name { get; private set; }
    public string Value { get; private set; }
}

public class Row {
    public IEnumerable<Cell> Cells { get; private set; }
}

public class ViewModel {
    public IEnumerable<Row> Rows { get; private set; }
}

<sdk:DataGrid ItemsSource="{Binding Rows}" />

如何使行/单元格查找正常工作并填充DataGrid?具体来说,我希望一旦 Rows 属性发生更改,网格就通过绑定进行更新(假设它引发了绑定响应的更改事件。)

I have a collection of objects, each of which holds a set of name-value pairs. The names are the same across all objects. I'd like to show these as columns in a data grid.

In Winforms/WPF I'd use ITypedList with some PropertyDescriptor instances to provide some fake properties to the runtime. However this type does not seem to be available in Silverlight.

So, is there an alternative, or does this not exist in Silverlight?

EDIT adding some code to frame the scenario better

public class Cell {
    public string Name { get; private set; }
    public string Value { get; private set; }
}

public class Row {
    public IEnumerable<Cell> Cells { get; private set; }
}

public class ViewModel {
    public IEnumerable<Row> Rows { get; private set; }
}

<sdk:DataGrid ItemsSource="{Binding Rows}" />

How can I get the row/cell lookup to work and populate the DataGrid? Specifically I want the grid to update via binding once the Rows property changes (assume it raises a change event that the binding responds to.)

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

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

发布评论

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

评论(1

噩梦成真你也成魔 2024-12-13 06:45:22

最后,我能够通过使用绑定和字符串索引器解决这个问题。

public class Row {
    public RowData Data { get; private set; }
}

public class RowData {
    public string this[string name] {
        get { return ...; }
    }
}

然后手动构建网格列:

foreach (var column in Columns)
{
    _grid.Columns.Add(new DataGridTextColumn
    {
        Binding = new Binding(string.Format("Data[{0}]", column.Name)),
        Header = column.Name,
        IsReadOnly = true
    });
}

这意味着数据会自动更新,因为在我的例子中,整个 Data 属性被替换,并且实现了 INotifyPropertyChanged 来通知绑定。

In the end I was able to solve this issue by using bindings and a string indexer.

public class Row {
    public RowData Data { get; private set; }
}

public class RowData {
    public string this[string name] {
        get { return ...; }
    }
}

Then build the grid columns manually:

foreach (var column in Columns)
{
    _grid.Columns.Add(new DataGridTextColumn
    {
        Binding = new Binding(string.Format("Data[{0}]", column.Name)),
        Header = column.Name,
        IsReadOnly = true
    });
}

This meant that the data updated automatically, because in my case the entire Data property was replaced, and INotifyPropertyChanged implemented to notify the binding.

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