سؤال

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

هل كانت مفيدة؟

المحلول

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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top