How do I handle Canvas.Top change event in WPF?
Asked Answered
P

1

12

I have an element positioned on Canvas using attached properties Canvas.Top and Canvas.Left. Then using animations the element is moved to different set of coordinates, like this:

DoubleAnimation left = new DoubleAnimation( oldLeft, newLeft );
DoubleAnimation top = new DoubleAnimation( oldTop, newTop );

element.BeginAnimation( Canvas.LeftProperty, left );
element.BeginAnimation( Canvas.TopProperty, top );

Is there a way to receive events whenever Canvas.Top or Canvas.Left is changed? Preferably without relation to animation.

Pitman answered 3/2, 2010 at 14:31 Comment(0)
P
24

One can catch attached property changed event using DependencyPropertyDescriptor's AddValueChanged method:

var descriptor 
    = DependencyPropertyDescriptor.FromProperty( 
        Canvas.LeftProperty, typeof( YourControlType ) 
      );
descriptor.AddValueChanged( this, OnCanvasLeftChanged );
Pitman answered 5/2, 2010 at 14:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.