1

I hope I didn't oversee an old question that's has stated the same, but as far as I can see they were all more specialized.

I wanted to know, if I am "on the right track". In the past I did some WinForms stuff with C# (mainly .net-3.5) and now I want to use / learn WPF for a little side-project. At the moment I am reading much about MVVM and other implementing-details when using WPF (.net4).

What I need to achieve is:

I want to have a kind of searchtool for an oracle-database. The Database-connection and so on is fixed, up and running. So no work to do on this part. The new tool should be ran on a windows7-desktop-pc and a windows7-tablet-pc. So I want/need two different Views one usual mouse-keyboard-interaction-gui and one optimized for touch-input.

So I had the idea of using one Model, one View-Model and two Views. I wouldn't want to use IOC via Unity or something of this kind as this would add a layer of complexity that would be overkill for this little side-project.

My actual Question:

Is this the right approach (using 2 Views with one View-Model) or am I completely wrong here (maybe DataTemplates would be better here?)? And what are good approaches to have this implemented?

Thanks in advance!

basti
  • 2,649
  • 3
  • 31
  • 46
  • why would you use 2 views? use one, make it somewhat flexible to use available space. works well. – Mare Infinitus Jun 11 '12 at 19:44
  • I wanted to have 2 different views to have "maximum" user-experience while using touch vs. mouse. For example showning the result-list in a datagrid in mouse-environement and a listbox (or simliar) with "big detailed" items (hard to explain...) when using touch-input... – basti Jun 11 '12 at 19:48
  • but the basic wiring of your view will stay the same, you just will use different coating. – Mare Infinitus Jun 11 '12 at 19:54
  • Yes. That's my question. Is it correct to use two views, or is there some other technique? Or do I "break" the pattern by doing it this way? – basti Jun 11 '12 at 19:55
  • then you can use MVVM that way. but in most cases it is not necessary to do something like that. getting a better user experience is always a good starting point for any developer :) – Mare Infinitus Jun 11 '12 at 20:06

3 Answers3

2

The idea behind having a ViewModel would be to have a place to consolidate view-related logic in a place that has no direct dependencies to view-specific stuff. So, yes, I'd say your approach is fine.

DataTemplates seem more suited for "smaller" View Models, so while a DataTemplateSelector could nicely do the job of selecting a Template based on your circumstances I think it's worth investing some extra code that can do that for you for full-sized Controls.

You will have to figure out whether you can automatically detect the way your users are using the app and maybe have your view names follow a convention such that choice of view can happen more or less automatically.

flq
  • 22,247
  • 8
  • 55
  • 77
  • Thanks for the nice answer. I thought of deciding which View to use by 2 things: Having it forced by the developer to show one specific view _and_ using some technique to determine if touch is enbaled like in http://stackoverflow.com/a/6352808/474221 – basti Jun 11 '12 at 20:02
1

I think you're on the right track.

This is a perfect example of the use of MVVM. You can change the view or have multiple appearances of the view, without having to change the backend.

I could see maybe using DataTemplates and a DataTemplateSelector, but if your views gain complexity then readability and maintainability will deteriorate.

Josh
  • 2,955
  • 1
  • 19
  • 28
0

I would also agree that you're on the right track but for a bigger project with nested user controls or many, many views it would be worth looking at a single View with two different styles. At it's simplest, the touch style would have bigger input controls. This would mean a change to binding logic would only have to be made in one place (the single View) instead of both Views that it is used in.

pete the pagan-gerbil
  • 3,136
  • 2
  • 28
  • 49
  • I understand your point, but the touch-view will `look` completely different than the mouse-view. So it will be much easier for me to use two different views. And is this not what MVVM is for? Replacing presentation and reusing the exisiting (data)model? – basti Jun 12 '12 at 09:39
  • I agree - but it depends on how much different the touch-view is. It may only be usability changes (fonts, button sizes, etc) in which case a style would be better. If it's whole layouts, then a whole view change would be better. A style would be easier to deal with in complex situations, and to maintain if the binding changed, as long as the layout stayed the same (though some layout you can set in styles anyway...) What I mean is, there's options and this one hadn't been presented yet! – pete the pagan-gerbil Jun 12 '12 at 10:00