附加属性更改事件? [英] Attached Property Changed Event?

查看:123
本文介绍了附加属性更改事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

北京时间有没有办法,如果附加属性改为得到变更通知?

ist there a way to get a change notification if an attached property changed?

一个简单的例子是一个画布长方形在里面。的位置 Rectange 通过使用DepenendyProperties设置 Canvas.Top Canvas.Left 。我使用的是装饰器通过改变 Canvas.Top Canvas.Left 四处移动矩形。

A simple example is a Canvas with a Rectangle in it. The position of the Rectange is set by using the DepenendyProperties Canvas.Top and Canvas.Left. I'm using an Adorner to move the Rectangle around by changing the Canvas.Top and Canvas.Left.

<Canvas Width="500" Height="500" >
   <Rectangle Width="40" Height="40" Canvas.Left="10" Canvas.Top="20" />
</Canvas>

下一步是两个长方形。为了跟踪移动的长方形箭必须得到变更通知每当矩形的位置的变化。这将是容易的,如果我能得到一个改变的通知时,附加属性 Canvas.Top 的变化。

The next step is to create an Arrow between two Rectangles. In order to keep track of the moving Rectangles the Arrow must get a change notification whenever the position of a Rectanglechanges. This would be easy if I could just get a changed notification when the Attached Property Canvas.Topchanges.

谢谢任何帮助,
迈克尔

Thanks for any help, Michael

推荐答案

你为什么不使用绑定?这正是他们设计的...

Why don't you use a binding ? That's precisely what they're designed for...

如果由于某种原因,你不能使用绑定,你可以添加一个处理程序通知时,的属性更改值:

If, for some reason, you can't use a binding, you can add a handler to be notified when the value of the property changes :

var topDescriptor = DependencyPropertyDescriptor.FromProperty(Canvas.TopProperty, typeof(Rectangle));
var leftDescriptor = DependencyPropertyDescriptor.FromProperty(Canvas.LeftProperty, typeof(Rectangle));
topDescriptor.AddValueChanged(rectangle, rectangle_PositionChanged);
leftDescriptor.AddValueChanged(rectangle, rectangle_PositionChanged);

...

private void rectangle_PositionChanged(object sender, EventArgs e)
{
    ...
}

这篇关于附加属性更改事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆