【问题标题】:C# bind multiple fields to ButtonC#将多个字段绑定到Button
【发布时间】:2015-11-07 05:38:00
【问题描述】:

我需要使用给定的字符串格式将多个属性绑定到 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++;
}

我有三个文本框和内部按钮,我需要以给定的格式组合它们:“{0},{1} {2}”。我正在为 Windows 8.1 平板电脑使用 WinRT。谢谢!

【问题讨论】:

  • 没什么可做的,但我想说将一个属性绑定到文本块。然后,如果您的任何依赖属性更新,请更新绑定属性,它只返回字符串格式的属性一起
  • +1 琼斯波利斯,这里没有足够的信息来回答您的问题。请重新表述您的问题,以便我们知道您绑定了多个文本块,而不仅仅是一个,提供一些您所拥有的示例代码,并明确您正在寻找什么。谢谢!
  • @AaronHawkins 我更新了问题并添加了源代码

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


【解决方案1】:

像这样使用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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-18
    • 2010-11-11
    • 2020-12-07
    相关资源
    最近更新 更多