-1

I need to bind multiple properties into TextBlock with given string format. Because this part of application is created dynamically I need to do it from code-behind.

Button button = new Button();
int row = 0;
foreach (KeyValuePair<string,string> item in component.Parameters.Value){

  TextBox textBox = new TextBox();
  textBox.Tag = control;

  string bindingKey = $"Data[{item.Value}]";
  textBox.SetBinding(TextBox.TextProperty, new Binding() { Path = new PropertyPath(bindingKey), Mode = BindingMode.TwoWay, UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged };);(textBox, bindingKey);

  row++;
}

I have three TextBoxes and inside button I need to combine all of them in given format: "{0}, {1} {2}". I am using WinRT for Windows 8.1 tablets. Thank you!

Vodáček
  • 254
  • 3
  • 12
  • 2
    not much to go off, but I'd say bind one property to the textblock. Then if any of your depended properties update, update the bound property, which just returns the string formatted properties together – Jonesopolis Nov 06 '15 at 19:53
  • 1
    +1 Jonesopolis, there is not enough information here to answer your question. Please rephrase your question so that we know there are multiple textblocks that you are binding to and not just one, provide some sample code of what you have, and clarify what you're looking for exactly. Thanks! – Aaron Hawkins Nov 06 '15 at 20:07
  • @AaronHawkins I have updated question and added source code – Vodáček Nov 06 '15 at 20:18

1 Answers1

0

Use the StringFormat property of the Binding object like so:

textBox.SetBinding(TextBox.TextProperty, 
    new Binding() 
    { 
        Path = new PropertyPath(bindingKey), 
        Mode = BindingMode.TwoWay, 
        UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged,
        StringFormat = "{0}, {1} {2}" 
     };);(textBox, bindingKey);

Reference:BindingBase.StringFormat

Aaron Hawkins
  • 2,611
  • 1
  • 20
  • 24