0

I wish to display some vector graphic and let the user select items.

For augment sake assume I am displaying an office plan that contains

  • Walls,
  • Corners,
  • Doors,
  • Windows,
  • Desks

I wish to be able to write tests against the view model to check both the “logical” and “physical” layout. E.g. I wish to be able to write a test that confirm the walls meets the corner as well as tests that confirm how they are displayed.

I also need to test commands like “insert door into the selected wall”

Has anyone tried doing this?

What are the issues with data binding to graphic items?

iCollect.it Ltd
  • 92,391
  • 25
  • 181
  • 202
Ian Ringrose
  • 51,220
  • 55
  • 213
  • 317
  • 1
    'Where do I start' questions are very hard to answer. Try it, and feel free to ask any specific questions you have. – Kendall Frey May 01 '12 at 15:54
  • @Kendall, Agreed, but an answer that points to a blog or some other real would experience doing it is also of great value. – Ian Ringrose May 01 '12 at 15:58
  • see also http://stackoverflow.com/questions/889825/is-it-possible-to-bind-a-canvass-children-property-in-xaml – Ian Ringrose May 01 '12 at 16:01

2 Answers2

1

Update: The more I think about it the more I'm leaning towards a custom control with good data binding support (important for MVVM). There are a lot of sources on the net covering this.

A semi answer to the question is there are two problems. The first is the display of complex data and exposing binding support, the second is MVVM practices on binding to it (but this becomes straightforward as the bulk of the problem is in the drawing). How does MVVM do vector graphics? It doesn't, the control does. How does the control expose what it needs in an extensible/generic way? Not sure, but the WPF Control Toolkit libraries on CodePlex will have some more involved examples.

This isn't really a "here you go" answer, but from a mindset perspective I think an important thing to consider is the ViewModel shouldn't care how it is "shown" (as in, ViewModel does not know View).

However, that isn't to say the ViewModel should not care what Model is presented.

A good Model structure for representing an office plan (with properties such as height, x/y coordinates, etc) is the important part. The ViewModel can then validate the model, the view simply responds to such validation results. This obviously creates a coupling between the ViewModel and the Model - I'm not sure what the "rules" are there.

The visual representation of this then hangs off the Model, and commands such as "insert door" can be solved with standard MVVM routed-command style coding. In your case, the ViewModel response would be to insert a new Door to the Office Plan model at the associated CurrentSelectedWall.

Or something like that.

The question I can't answer is: is the perceived tight coupling between the View and the Model allowed? In my sample, the Model would hold all the key information for the View to draw the shapes (height, x/y etc). Perhaps this can be abstracted out.

Unfortunately, creating the view likely won't be possible with simple data binding in XAML - I can only see a view-code-heavy way to do this... :-(

The alternative is a custom control that exposes the data it needs and provides data binding points to receive that data.

Adam Houldsworth
  • 63,413
  • 11
  • 150
  • 187
1

I'm going to do more or less the same thing. I do not have realized yet but my idea was to have a ViewModelCanvas where I put the logic and where the contained objects are WallViewModel, DoorViewModel etc.. Each of those object contains coordiantes, orientation and other properties. Then in the view layer I have a WPFCanvas with ViewModelCanvas as Datacontext and a template for each viewmodel class that define how it should be appear.

It should work for static visualization. Not sure if it is enough flexible to have an interactive view however there is a good probability ;)

Roberto
  • 504
  • 11
  • 23