Jaa


DependencyProperty Precedence(3)

Let’s focused on the coercion of DP value.

In the precedence list, the coercion source is the highest.

In order to verify it, I have created a class called CoercionButton.

    1: public class CoercionButton : Button
    2: {
    3:     static CoercionButton()
    4:     {
    5:         WidthProperty.OverrideMetadata(typeof(CoercionButton), new FrameworkPropertyMetadata(null, OnCoerceValueCallback));
    6:     }
    7:  
    8:     // public delegate object CoerceValueCallback(DependencyObject d, object baseValue);
    9:     public static object OnCoerceValueCallback(DependencyObject d, object baseValue)
   10:     {
   11:         return 150.0;
   12:     }
   13: }

It overrides the Metadata of WidthProperty and adds CoerceValueCallback. And in the callback, returns a constant value 150.0.

We change the “Target Button” from normal Button type to CoercionButton type.

Main-1

But the event handler for other 4 buttons remain some.

No matter which button you clicks, the Width of “Target Button” is always 150.

And the animation doesn’t work.

Comments