简体   繁体   中英

Silverlight make a column bound to a array item editable

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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