问题
I have a DataGrid
in my Silverlight application, which is bound to an array of objects. Since the columns will be variable, I bound each of them to an array item:
My object:
public class TravelTimeItem
{
public string From { get; set; }
public int[] Times { get; set; }
}
I build my grid with that:
grdTravelTime.Columns.Clear();
grdTravelTime.Columns.Add(new DataGridTextColumn() {
Binding = new Binding("From")
});
for (int i=0; i < amountOfColumns; i++)
grdTravelTime.Columns.Add(new DataGridTextColumn()
{
Binding = new Binding("Times[" + i.ToString() + "]"),
Header = (i + 1).ToString()
});
grdTravelTime.AutoGenerateColumns = false;
grdTravelTime.IsReadOnly = false;
grdTravelTime.ItemsSource = GetItems();
The code above is working, I can see all array values displayed correctly in the grid, but I need the values to be editable. The "From" column, bound to a simple STring property, is editable, but the others, bound to a array item, is not.
How can I make this columns editable? Is there other option to bound this columns, taking into account the amount of columns can be different each time.
回答1:
How can I make this columns editable?
You need to replace int
with a custom type that has a property with a public setter:
public class TravelTimeItem
{
public string From { get; set; }
public YourType[] Times { get; set; }
}
public class YourType
{
public int Value { get; set; }
}
...
for (int i = 0; i < amountOfColumns; i++)
grdTravelTime.Columns.Add(new DataGridTextColumn()
{
Binding = new Binding("Times[" + i.ToString() + "].Value"),
Header = (i + 1).ToString()
});
来源:https://stackoverflow.com/questions/43374132/silverlight-make-a-column-bound-to-a-array-item-editable