2016-09-22 65 views
0

如何在不使用XAML的情況下在WPF中嵌套目標屬性(如Shape.Stroke.Color)上的綁定?在不使用XAML的情況下在嵌套屬性上綁定

對於一個簡單的屬性我使用看起來像這樣的代碼:

var binding = new Binding("mySourceProperty"); 
binding.Source = mySourceObject; 
myTargetObject.SetBinding(myTargetProperty, binding); 

哪裏myTargetProperty可以,例如,Shape.StrokeProperty。 但是現在,我怎樣才能在ShapeStrokeColorProperty上做同樣的事?

回答

0

只要形狀的Stroke屬性包含的SolidColorBrush,您可以使用靜態BindingOperations.SetBinding方法:

var shape = new Path(); // or whatever 
var binding = new Binding { Source = Colors.Red }; // or whatever 
BindingOperations.SetBinding(shape.Stroke, SolidColorBrush.ColorProperty, binding); 
+0

謝謝了'BindingOperations.SetBinding()'是我錯過了什麼。 –