【问题标题】:Silverlight data binding in the code behindSilverlight 数据绑定在后面的代码中
【发布时间】:2009-10-08 22:08:03
【问题描述】:

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

而且效果很好,但我只想将 ready 更改为更像: TextColumn.IsReadOnly = new Binding(String.Format("Periods[{0}].IsReadOnly", x));

虽然在 XAML 中似乎很容易做到,但我无法在后面的代码中找出正确的方法来做到这一点。显然我不能将它设置为“绑定”,但我可以在哪里设置这样的东西?

编辑#1:

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

【问题讨论】:

    标签: silverlight data-binding xaml


    【解决方案1】:
    BindingOperations.SetBinding(textColumn, DataGridTextColumn.IsReadOnlyProperty, new Binding(...));
    

    【讨论】:

    • 这太棒了,解决了我遇到的一般理解问题,但 DataGridTextColumn 没有 IsReadOnlyProperty。
    • DataGridTextColumn 有一个 IsReadOnly 属性,继承自 DataGridColumn,但从文档中不清楚它是否是依赖属性(我手头没有检查它的工具,抱歉)——文档没有像您对 DP 所期望的那样提及 IsReadOnlyProperty 字段。如果不是 DP,则不能进行数据绑定。因此,您可能需要改用 DataGridTemplateColumn,并且您的 DataTemplate 由 TextBox 组成;然后,您可以绑定该 TextBox 的 IsReadOnlyProperty。
    • @itowlson 你能解释一下DataGridTemplateColumn 的方法吗?如何访问里面的文本框?如何在代码中设置绑定?
    • @HosseinNarimaniRad Urgh,抱歉;我已经四年没有看过 Silverlight 了,甚至不再安装开发工具来提醒自己。如果您在 WPF 对我执行 ping 操作,但我对 Silverlight 无能为力,抱歉。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多