【问题标题】:Programmatically bind to 2D observable collection以编程方式绑定到 2D 可观察集合
【发布时间】:2023-02-14 01:09:35
【问题描述】:

我有 ObservableCollection<ObservableCollection<string>>,我正在尝试将每个字符串绑定到。

这是我试过的。

txtBlock = new TextBox();
Binding txtBind = new Binding();
txtBind.Source = exportData;
txtBind.Path = new PropertyPath("exportData", i, j);
                    //txtBlock.Text = exportData[i][j];
BindingOperations.SetBinding(txtBlock, TextBlock.TextProperty, txtBind);

它没有填充文本框。

【问题讨论】:

    标签: c# wpf


    【解决方案1】:

    我很少在代码中编写绑定。据我了解,propertypath 应该与您在 xaml 中使用的字符串相同。

    您可以使用字符串插值来安排它。

    因此

     txtBind.Path = new PropertyPath($"exportData[{i}][{j}]");
    

    假设 exportData 是您的二维集合。

    不要设置源。

    确保文本块的数据上下文是一个具有 exportData 公共属性的视图模型。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-05-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-03
      • 2013-05-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多