2009-10-08 69 views
1

我做这样的事情的代码绑定:Silverlight的数据在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。有没有办法注入/添加一个?

回答

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

这是真棒,并解决了我有一般的理解问题,但DataGridTextColumn没有IsReadOnlyProperty。 – JasonRShaver 2009-10-09 15:25:13

+0

DataGridTextColumn具有从DataGridColumn继承的IsReadOnly属性,但从文档中不清楚它是否是依赖属性(并且我没有手头的工具来检查它,对不起) - 文档没有提及IsReadOnlyProperty字段与您对DP所期望的一样。如果它不是DP,则它不能被数据绑定。因此,您可能需要使用DataGridTemplateColumn,并使用由TextBox组成的DataTemplate;然后您可以绑定该TextBox的IsReadOnlyProperty。 – itowlson 2009-10-09 18:23:31

+0

@itowlson你能解释一下'DataGridTemplateColumn'的方法吗?如何访问里面的文本框?如何在代码中设置绑定? – 2014-04-09 06:32:51