附加属性更改事件? [英] Attached Property Changed Event?
问题描述
北京时间有没有办法,如果附加属性改为得到变更通知?
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>
下一步是两个长方形$ C之间创建一个箭头$ C>。为了跟踪移动的
长方形
箭必须得到变更通知每当矩形的位置
的变化。这将是容易的,如果我能得到一个改变的通知时,附加属性 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 Rectangle
changes. This would be easy if I could just get a changed notification when the Attached Property Canvas.Top
changes.
谢谢任何帮助,
迈克尔
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屋!