I struggle with binding FillProperty in a classe derived from Shape.
public static readonly DependencyProperty NumberNodeProperty = DependencyProperty.Register("Number", typeof(int), typeof(MyDerivedShape), new FrameworkPropertyMetadata(0, FrameworkPropertyMetadataOptions.AffectsMeasure | FrameworkPropertyMetadataOptions.AffectsParentMeasure));
public MyDerivedShape( DerivedViewModel viewmModel):Shape
{
DataContext = viewmModel;
Binding FillColorBinding = new Binding("FillColor");
SetBinding(FillProperty, FillColorBinding);
Binding numberBinding = new Binding("Number");
SetBinding(NumberNodeProperty, numberBinding);
}
"FillColor" property is declared in a base Viewmodel, from which DerivedViewModel inherits.
"Number" property is declared in DerivedViewModel
FillProperty is the default dependency property in Shape base class.
NumberNodeProperty is a dependency property declared in MyDerivedShape
So , what happens is that when I change "Number" in the DerivedViewModel, change is propagated to the Shape (the Shape draws a number)
But when I change FillColor in the DerivedVie开发者_如何学运维wModel, Change is not propagated, and color is not changed . I use FillColor is of type SolidColorBrush.
It seems that the binding does not work... Is it a consequence "Inherits" property set to false for "FillProperty" depedency property ?
I answer myself, because I found the answer :
In fact , in another piece of code in the application , I do something like this :
MyDerivedShape.Fill = Brushed.Red
This has huge consequences,as It destroys the Binding I put in place !
It is connected to "Dependency Property Value Precedence", a topic I was not aware at all.
So, if you bind a ViewModel Property to Dependency Property, you should never then set the Dependency Property directly. If you do so, your binding is lost !
精彩评论