Click here to Skip to main content
Sign Up to vote bad
good
See more: C#WPF
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
Posted 28-Feb-12 19:59pm

Comments
SAKryukov - 29-Feb-12 2:10am
In canvas properties or its children, too? --SA
AmrutaAsbe - 29-Feb-12 2:20am
Its children too..e.g if a rectangle already drawn and I change its color throw a message.Right now I am thinking of creating a class and every event will increase some version number,but there are lot of events from color change, fill color,position change, drag and drop of new item,deleting old ones.So honestly I don't find it a right approach.
SAKryukov - 29-Feb-12 2:34am
I actually assumed that and started to answer before I saw this comment. I agree with you. Please check my answer; but it may need some more thinking. With this comment, I decided to up-vote your question with my 5 as a quite interesting one (believe me, I up-vote questions very, very rarely :-). --SA

2 solutions

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
  Permalink  
Comments
Abhinav S - 29-Feb-12 3:24am
My 5.
SAKryukov - 29-Feb-12 3:40am
Thank you, Abhinav. --SA
AmrutaAsbe - 29-Feb-12 22:33pm
Thank you guys for quick replies.Abhinav I am looking more than just layout change but thanks ,is a good tip for future.SAKryukov u managed to understand what exactly I wanted,So I am going for wrapper class I got the idea from the links which made my thinking a bit more clear. :)
AmrutaAsbe - 29-Feb-12 22:35pm
btw My 5 to both for quick replies,but SAKryukov u made my day so would love to give more than 5 ..not sure how to do that.. so u have to be satisfied with 5 here.
SAKryukov - 1-Mar-12 0:05am
You are very welcome. I suggest you accept my answer formally (green button) -- thanks. --SA
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.
  Permalink  
Comments
SAKryukov - 29-Feb-12 2:32am
This is a right idea (surely my 5). Maybe this is what OP wants. But I assumed that the change in the children themselves (and not just attached properties, not just attached to the canvas) need to send notifications. So, I tried to bring some ideas in my answer. What do you think? --SA
Abhinav S - 29-Feb-12 3:24am
Thank you SA. The OP confirmed your assumption.
SAKryukov - 29-Feb-12 2:36am
Actually, OP just confirmed my assumption, so please see. --SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Christian Graus 578
1 Ron Beyer 326
2 Tadit Dash 253
3 samadhan_kshirsagar 229
4 OriginalGriff 198
0 Sergey Alexandrovich Kryukov 7,041
1 Prasad_Kulkarni 3,815
2 OriginalGriff 3,557
3 _Amy 3,372
4 CPallini 3,034


Advertise | Privacy | Mobile
Web01 | 2.6.130619.1 | Last Updated 29 Feb 2012
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid