Associate WPF Graphic with a Class

W

Wonko the Sane

Hi All,

I have the following (much-simplified) situation. I have a class, part of
which involves drawing graphics on a Canvas. I may have a number of
instances of these graphics, which may include a line, two rectangles used as
endpoints, and perhaps a label for the line.

I want to somehow later associate a graphic on the canvas with an instance
of the class. For instance, if the user selects an endpoint and drags it, I
want to resize the line, and perhaps update some information in a database
based on that resize.

I'm just looking for a graceful way to do this. I have a solution, but it
seems inelegant. Any advice or a pointer to an example would be
appreciated...

Thanks.
 
N

Nicholas Paldino [.NET/C# MVP]

Hmm, instead of drawing the lines and whatnot, have you considered
making the lines (with the associated elements) and rectangles UIElements
themselves? They would have their own dependency properties, etc, etc, and
then you would add them to the canvas.

Then, in relation to your class, you could just have it listen to the
event handlers for the dependency properties you expose and do something
when the value changes.

For example, for a Line class, you would have a Point1 dependency
property and a Point2 dependency property (maybe, it's up to you) which
expose the endpoints of the line. Then, you could just register to receive
change notifications in your class that you want to receive updates.
 
W

Walter Wang [MSFT]

I agree with Nicholas. The WPF-way of doing this is using UIElements on the
canvas. Josh Smith has created a customized Canvas that could let you move
around any UIElements on it.

http://www.codeproject.com/WPF/DraggingElementsInCanvas.asp

To resize an UIElement, you will need to use Adorner:

http://dvuyka.spaces.live.com/blog/cns!305B02907E9BE19A!144.entry

Hope this helps.


Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
W

Walter Wang [MSFT]

Hi,

I'm writing to check the status of this post. Please feel free to let me
know if there's anything else I can help. Thanks.


Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top