如果SourceDependencyObject是DependencyObject
,看來我們應該能夠從SourceDependencyProperty獲取名稱並設置爲Binding.Path
。
例如:
TextBlock SourceDependencyObject = new TextBlock();
SourceDependencyObject.Text = "Hello";
TextBlock TargetDependencyObject = new TextBlock();
Binding myBinding = new Binding();
myBinding.Source = SourceDependencyObject;
myBinding.Path = new PropertyPath("Text");
BindingOperations.SetBinding(TargetDependencyObject, TextBlock.TextProperty, myBinding);
Debug.WriteLine(TargetDependencyObject.Text);
如果我們不設置路徑時,TargetDependencyProperty將指向SourceDependencyObject。
所以如果SourceDependencyObject是string
,int
或其他類型。我們不需要設置Binding.Path。
例如:
TextBlock SourceDependencyObject = new TextBlock();
SourceDependencyObject.Text = "Hello";
TextBlock TargetDependencyObject = new TextBlock();
Binding myBinding = new Binding();
myBinding.Source = SourceDependencyObject.Text;
//myBinding.Path = new PropertyPath("Text");
BindingOperations.SetBinding(TargetDependencyObject, TextBlock.TextProperty, myBinding);
Debug.WriteLine(TargetDependencyObject.Text);
任何更新?我的答案解決了你的問題嗎? –