我結合其內容屬性正在嘗試在運行時創建一個標籤,並連接它的Content
財產到另一個TextBox
控制這是在我的UserControl
稱爲MyLabelSettings
。如何創建標籤,並動態地添加在WPF
這是我走到這一步:
Label currCtrl = new Label();
MyLabelSettings currCtrlProperties = new MyLabelSettings();
// Bindings to properties
Binding binding = new Binding();
binding.Source = currCtrlProperties.textBox_Text.Text;
binding.Path = new PropertyPath(Label.VisibilityProperty);
BindingOperations.SetBinding(currCtrl.Content, Label.ContentProperty, binding);
最後一行顯示了我也沒弄明白怎麼解決的錯誤:
爲「系統的最佳重載的方法匹配。 Windows.Data.BindingOperations。 SetBinding(System.Windows.DependencyObject,System.Windows.DependencyProperty,System.Windows.Data.BindingBase)」有一些無效參數
我在MyLabelSettings
的INotifyPropertyChanged
實施其具有下面的代碼時TexBox.Text
變化
private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
{
InvokePropertyChanged(new PropertyChangedEventArgs("TextChanged"));
}
有沒有更好的方法來綁定這2個?或者我在這件事上做錯了什麼?
謝謝!