Silverlight数据绑定在代码后面

时间:2009-10-08 22:08:03

标签: silverlight data-binding xaml

我在Silverlight 3数据网格中做了类似的事情:

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

它的效果很好,但是我想把现成的更改为更像:     TextColumn.IsReadOnly = new Binding(String.Format(“Periods [{0}]。IsReadOnly”,x));

虽然在XAML中看起来很容易,但我无法找到在后面的代码中执行此操作的正确方法。显然我无法将其设置为“绑定”,但我可以在哪里设置类似的内容?

编辑#1:

我查看了下面给出的BindingOperations.SetBinding(),但找不到IsReadOnly的DependencyProperty。有没有办法注入/添加一个?

1 个答案:

答案 0 :(得分:5)

BindingOperations.SetBinding(textColumn, DataGridTextColumn.IsReadOnlyProperty, new Binding(...));