2015-11-06 46 views
-1

我需要使用給定的字符串格式將多個屬性綁定到TextBlock中。因爲這部分應用程序是動態創建的,所以我需要從代碼隱藏中完成。C#將多個字段綁定到按鈕

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}」。我正在使用WinRT for Windows 8.1平板電腦。謝謝!

+2

不多熄滅,但我要說綁定一個屬性的文本塊。然後,如果你的任何依賴屬性更新,更新綁定屬性,它只是返回字符串格式化的屬性 – Jonesopolis

+1

+1 Jonesopolis,沒有足夠的信息來回答你的問題。請重新說明您的問題,以便我們知道您綁定了多個文本塊,而不僅僅是一個,提供一些示例代碼,說明您正在尋找什麼。謝謝! –

+0

@AaronHawkins我已更新問題並添加了源代碼 –

回答

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