开发者

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

开发者 https://www.devze.com 2022-12-18 19:25 出处:网络
I have an element positioned on Canvas using attach开发者_运维知识库ed properties Canvas.Top and Canvas.Left. Then using animations the element is moved to different set of coordinates, like this:

I have an element positioned on Canvas using attach开发者_运维知识库ed 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.


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

var descriptor 
    = DependencyPropertyDescriptor.FromProperty( 
        Canvas.LeftProperty, typeof( YourControlType ) 
      );
descriptor.AddValueChanged( this, OnCanvasLeftChanged );
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号