Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hi How can I add objects to a canvas and then move the objects around in MVVM style?
I managed to do it just in a small WPF project.
In MVVM I just see the class name of the object, but no image.
Anyone a small example?
Thanks
Posted

1 solution

I think defining Canvas children and their location via XAML is pretty obvious. If the problem is doing it programmatically, this is what it is:

Adding: The property Canvas.Children is of the type UIElementsCollection; it has all required methods, such as Add(UIElement), Clear(), and so on:
https://msdn.microsoft.com/en-us/library/system.windows.controls.canvas%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/system.windows.controls.panel.children%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/system.windows.controls.uielementcollection%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/ms603589(v=vs.110).aspx[^].

Moving could be a bit more confusing if you are unfamiliar with dependency properties. This is done by using the 4 (or just 2) Canvas.Set*(UIElement, double) methods:
https://msdn.microsoft.com/en-us/library/system.windows.controls.canvas.setright(v=vs.110).aspx[^],
https://msdn.microsoft.com/en-us/library/system.windows.controls.canvas.settop(v=vs.110).aspx[^],
https://msdn.microsoft.com/en-us/library/system.windows.controls.canvas.setright(v=vs.110).aspx[^],
https://msdn.microsoft.com/en-us/library/system.windows.controls.canvas.setbottom(v=vs.110).aspx[^].

The generalized by untyped methods of setting dependency property values are these two:
https://msdn.microsoft.com/en-us/library/ms597473(v=vs.110).aspx[^],
https://msdn.microsoft.com/en-us/library/ms597474(v=vs.110).aspx[^].

See also: https://msdn.microsoft.com/en-us/library/vstudio/ms752914%28v=vs.100%29.aspx[^].

—SA
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900