监视画布中的更改 [英] Monitor change in canvas

查看:165
本文介绍了监视画布中的更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我想跟踪画布中发生的更改.
Canvas.Children.Count并不是那么有用,它只是计算项目的数量,不确定在这里其他哪个属性对我有帮助.
像这样的东西.
如果(在画布中更改(可以是任何位置.已移动位置,更改了颜色.添加/删除了新项目))
{
做某事
}
其他

{
做某事
}

我希望我清楚...
在此先感谢

Hello people,

I want to keep a track of changes happening in my canvas.
Canvas.Children.Count is not so helpful it just counts the number of items ,not sure which other property will be helpful for me here.
something like this.
if(change in canvas(can be anything.. position moved,color change..new item added/deleted))
{
do something
}
else

{
do something
}

I hope I am clear...
Thanks in advance

推荐答案

您可以使用LayoutUpdated 在画布中拖动元素 [
You can use the LayoutUpdated event[^] to monitor any changes within the Canvas.
The drawback is this event would fire every-time the page refreshes.

Dragging Elements in a Canvas[^] could actually help you formulate your own logic to handle the problem.


第一个琐碎的想法是:您可以始终将画布包装在某些包装器 facade 类中,在该类中,您感兴趣的所有操作都将带有所需的通知.您只需要向包装类添加适当的事件,然后在这些操作上调用它们即可.

(请参阅: http://en.wikipedia.org/wiki/Facade_pattern [ http://msdn.microsoft.com/en-us/library/ms745811.aspx [ ^ ].

(有关依赖项属性和附加属性的更多信息,请参见:
http://msdn.microsoft.com/en-us/library/ms752914.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/ms749011.aspx [ ^ ].

此CodeProject文章也可能有用: WPF教程-依赖属性 [ ^ ].)
关于子级的其他属性,您可以使用负责将子级添加到包装的画布实例中的门面方法,使用"+ ="运算符,使外观直接向子级添加适当的事件处理程序.

这一切都需要一些思考,但是我认为您可以理解这些想法.

—SA
First trivial idea is: you can always wrap the canvas in some wrapper facade class where all the operations you are interested in comes with notifications you need. You should simply add appropriate events to the wrapper class and invoke them on those operations.

(Please see: http://en.wikipedia.org/wiki/Facade_pattern[^].)
If you need to get notifications on some or all properties of the canvas children, it''s a bit more complex. First of all, some properties like child position are attached properties, so you track then via a canvas (and hence your facade wrapper class). Please see:
http://msdn.microsoft.com/en-us/library/ms745811.aspx[^].

(For more information on dependency properties and attached properties, please see:
http://msdn.microsoft.com/en-us/library/ms752914.aspx[^],
http://msdn.microsoft.com/en-us/library/ms749011.aspx[^].

This CodeProject article can also be useful: WPF Tutorial - Dependency Property[^].)
As to the other properties of the children, you can make the facade adding appropriate event handlers to the children on the fly, using "+=" operator, by the facade methods responsible for adding children to a canvas instance wrapped.

This all needs some thinking, but I think you can understand the ideas.

—SA


这篇关于监视画布中的更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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