2

I have a ComboBox in my View which the user choose a number of TextBox's to create dynamically. each TextBox has an Index i, and I want to bind each of them to array[i]. I searched for a solution and I found that I need to use MultiBinding with a converter.

How can I set a MultiBinding for a TextBox in my C# Code, and not in XAML ? 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. 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]"?

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

Cœur
  • 37,241
  • 25
  • 195
  • 267
MatanKri
  • 263
  • 1
  • 3
  • 13
  • It seems odd that you would want to bind a single textbox to multiple array items, you most likely want to bind to a single array item, so a multibinding isn't necessary. – slugster Jun 15 '14 at 20:50
  • I want to bind a single Textbox to a single array item, in order to bind it with array[i], I need a converter. I would be happy to get an alternative solution though. – MatanKri Jun 15 '14 at 21:05
  • Provide the code on how you're creating the textboxes. How is the index (i) exposed? The text binding can bind directly to the array instance. for example Text="{Binding array[0]}" With that being said, explain what you're trying to accomplish, there is probably a better way than the approach you're trying now. – Michael G Jun 15 '14 at 21:11
  • No you don't need a converter, you can just specify an indexer on the binding. You only need a converter to change data being bound to. – slugster Jun 15 '14 at 21:27
  • Show the definition and initialization of the array property. – Michael G Jun 15 '14 at 21:53
  • @HighCore I need it to be dynamically, do you have a suggestion how to do it using XAML ? – MatanKri Jun 16 '14 at 06:41
  • @MatanKri use an `ItemsControl`. Creating or manipulating UI elements in procedural code in WPF is a bad practice. Use XAML instead. – Federico Berasategui Jun 16 '14 at 14:09
  • @HighCore Ok, could you give an example? – MatanKri Jun 17 '14 at 12:12
  • @MatanKri I've given tons of examples about this exact same thing already in SO. Perform a proper search. Otherwise post a screenshot of what you need and I can tell you the proper to do it in WPF. – Federico Berasategui Jun 17 '14 at 12:14
  • @MatanKri see [here](http://stackoverflow.com/a/15344546/643085) – Federico Berasategui Jun 17 '14 at 12:17

1 Answers1

0

I think you should write something as follows:

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