开发者

WPF-MVVM: Custom Control/User Control/POCO for Paint-like Application?

开发者 https://www.devze.com 2023-01-06 04:48 出处:网络
So I am currently creating a Paint-like application for sports. You will some \"shapes\" for each type of sport (balls/baskets in basketball, pucks/goals in hockey etc.).

So I am currently creating a Paint-like application for sports. You will some "shapes" for each type of sport (balls/baskets in basketball, pucks/goals in hockey etc.).

Now my main problem is how I should go开发者_运维技巧 about designing these shapes. As I understand it there are mainly 3 options:

  • Custom Control with ControlTemplate
  • UserControl
  • POCO-Object (ViewModel) for property storage, DataTemplate for visual representation

I have the following requirements for the Shape-objects:

  • Should be easily serializable (to save as XML etc.)
  • Should be able to have adorners attached to them (for purposes of resizing etc.)
  • Should be extensible/modifiable (provide different visual styles for them etc.)
  • Should provide some default value mechanism for the properties

Which option would fit my requirements best?


Since you have a clear domain model (balls, pucks, etc) I would recommend modelling those as separate objects (ViewModels) and then use DataTemplates to style them visually. That way you have the most control over what gets serialized and you can version your visuals independently from your data model.

Serialization will be handled by your data model. The DataTemplates might use custom shapes (just derive from Shape) to do the actual rendering. Adorners work on any UIElements and if you use data binding to bind the relevant properties back to your ViewModels you have the requried data to serialize. As for the mechanism for default values just let the default constructor on your Ball and Puck class set the proper values (diameter, weight, etc)

Happy coding

0

精彩评论

暂无评论...
验证码 换一张
取 消