C#将多个字段绑定到Button

时间:2015-11-06 19:50:39

标签: c# binding winrt-xaml code-behind

我需要使用给定的字符串格式将多个属性绑定到TextBlock中。因为这部分应用程序是动态创建的,所以我需要从代码隐藏中完成它。

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++;
}

我有三个TextBox和内部按钮我需要以给定的格式组合所有这些:&#34; {0},{1} {2}&#34;。我正在使用WinRT for Windows 8.1平板电脑。谢谢!

1 个答案:

答案 0 :(得分:0)

使用Binding对象的StringFormat属性,如下所示:

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

参考:BindingBase.StringFormat