views:

947

answers:

1

I am doing something like this in a Silverlight 3 datagrid:

for (int x = 0; x < ThisForecast.Periods.Count; x++)
{
   var TextColumn = new DataGridTextColumn();
   TextColumn.Header = ThisForecast.Periods[x].Name;
   TextColumn.Binding = new Binding(String.Format("Periods[{0}].Quantity", x));
   TextColumn.Binding.Mode = BindingMode.TwoWay;
   TextColumn.IsReadOnly = false;
   dgItemForecast.Columns.Add(TextColumn);
}

And it works great, but I want to change the ready only to something more like: TextColumn.IsReadOnly = new Binding(String.Format("Periods[{0}].IsReadOnly", x));

And while it seems easy to do in XAML, I can't figure out the correct method to do this in the code behind. Obviously I can't set it to a 'binding', but where would I be able to set something like that?

EDIT #1:

I looked at the BindingOperations.SetBinding() given below, but could not find a DependencyProperty for IsReadOnly. Is there a way to inject/add one?

+2  A: 
BindingOperations.SetBinding(textColumn, DataGridTextColumn.IsReadOnlyProperty, new Binding(...));
itowlson
That is awesome and addresses the general understanding issue I am having, but DataGridTextColumn does not have a IsReadOnlyProperty.
JasonRShaver
DataGridTextColumn has an IsReadOnly property, inherited from DataGridColumn, but it's not clear from the docs whether it's a dependency property (and I don't have the tools at hand to check it out, sorry) -- the docs don't mention an IsReadOnlyProperty field as you would expect for a DP. If it's not a DP, it can't be data bound. You may therefore need to use a DataGridTemplateColumn instead, and with your DataTemplate consisting of a TextBox; you can then bind the IsReadOnlyProperty of that TextBox.
itowlson