简体   繁体   English

WPF MultiBinding TextBox以编程方式编写文本

[英]WPF MultiBinding TextBox Text programmatically

I have a ComboBox in my View which the user choose a number of TextBox's to create dynamically. 我的视图中有一个ComboBox,用户可以选择多个TextBox来动态创建。 each TextBox has an Index i , and I want to bind each of them to array[i] . 每个TextBox都有一个索引i ,我想将它们每个都绑定到array[i] I searched for a solution and I found that I need to use MultiBinding with a converter. 我搜索了一个解决方案,发现需要将MultiBinding与转换器一起使用。

How can I set a MultiBinding for a TextBox in my C# Code, and not in XAML ? 如何在C#代码而不是XAML中为TextBox设置MultiBinding? Thanks 谢谢

Edit: I am creating a grid of textBoxes, in order to create a matrix, and I want to bind all of them to one array that will contain this matrix as a vector. 编辑:我正在创建一个textBoxes网格,以便创建一个矩阵,并且我想将所有它们绑定到一个包含该矩阵作为矢量的数组。 my code: 我的代码:

myGrid.Children.Clear();
myGrid.RowDefinitions.Clear();
myGrid.ColumnDefinitions.Clear();
int row = int.Parse(comboBoxRow.SelectedValue.ToString());
int col = int.Parse(comboBoxCol.SelectedValue.ToString());

for (int i = 0; i < row; i++)
{
    myGrid.RowDefinitions.Add(new RowDefinition());
}
for (int i = 0; i < col; i++)
{
    myGrid.ColumnDefinitions.Add(new ColumnDefinition());
}

for (int i = 0; i < row; i++)
{
    for (int j = 0; j < col; j++)
    {
        TextBox tb = new TextBox();
        tb.SetValue(Grid.RowProperty, i);
        tb.SetValue(Grid.ColumnProperty, j);
        tb.SetBinding(TextBox.TextProperty, "matrixArray[j + col * i]")
        myGrid.Children.Add(tb);
    }
}

So what do I need to write instead of: "array[j + col * i]" ? 那么我需要写些什么而不是: "array[j + col * i]"

in ViewModel: public string[] matrixArray{ get; set; } 在ViewModel中: public string[] matrixArray{ get; set; } public string[] matrixArray{ get; set; }

我认为您应该编写以下内容:

tb.SetBinding(TextBox.TextProperty, string.Format("matrixArray[{0}]", j + col * i))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM