views:

595

answers:

4

Hi, I'm currently working on a brownfield application, it's written with winforms, as a preparation to use WPF in a later version, out team plans to at least use the MVVM/Presentation model, and bind it against winforms...

I've explored the subject, including the posts in this site (which i love very much), when boiled down, the main advantage of wpf are :

  • binding controls to properties in xaml.
  • binding commands to command objects in the viewmodel.

the first feature is easy to implement (in code), or with a generic control binder, which binds all the controls in the form.

the second feature is a little harder to implement, but if you inherit from all your controls and add a command property (which is triggered by an internal event such as click), which is binded to a command instance in the ViewModel.

The challenges I'm currently aware of are :

  • implementing a commandmanager, (which will trigger the CanInvoke method of the commands as necessery.
  • winforms only supports one level of databinding : datasource, datamember, wpf is much more flexible.

am i missing any other major features that winforms lacks in comparison with wpf, when attempting to implement this design pattern?

i sure many of you will recommend some sort of MVP pattern, but MVVM/Presentation model is the way to go for me, because I'll want future WPF support.

Thanks in advance, Erik.

+1  A: 

Please take a look at Update Controls .NET. It is an open-source library for Winforms, WPF, and Silverlight that keeps controls up to date as data changes. You can start using it now for Winforms, and then transition over to WPF without changing your Data Model or View Model code.

Unfortunately, it does not solve the Winforms command binding problem. Your button click events will not port from Winforms to WPF. But it does take care of the data binding problem.

Michael L Perry
A: 

Erik,

I'm surprised you haven't been inundated with requests to share your findings.

I've spent quite a bit of time exploring WPF/MVVM and like what I see.

The trouble is, I too, have an existing WinForms code base that I can't simply throw away and start again.

Like you my thoughts are to convert all the logic to the view-model classes and, for now, wire those up to the existing forms. Then, when we're ready, rip out the WinForms and replace them with WPF views.

Like you, the bit I'm struggling with is the CommandManager and requerying the state of each command binding. How did you resolve this issue? did you find an acceptable work-around?

Many thanks.

Chris.

A: 

Hi All,

I find it assuring that there are more people on earth like me who are struggling with developing a WinForm code base with WPF in mind, to migrate to "View Model" with WPF in future. The challenges are rightly pointed out by Erik.

This is not an answer to the post rather one more challenge that I am facing, to bind Hierarchical Data (for treeview). Erik, were you referring to this when you said "...winforms only supports one level of databinding..." ?

I will invest just a little more energy to find an acceptable solution that can handle these challenges, at least partially. But after that I think I am going to stick to exposing "Methods" in the Presentation Model / View Model (whatever you call it) for calling from Command button click events.

For hierarchical data, I think for now I have to stick to generating the TreeView nodes in "View" (in a Windows Form to be precise) based on a dataset from the "View Model/ Presentation Model" and "synchronize" the currently selected node to a property e.g. "SelectedMenuId" in the "View Model". This needs a little more code since when I say "synchronize" I mean handling a "AfterSelect" event of the TreeView for example, and manually put some code like...

    private void TreeView1_AfterSelect(System.Object sender, 
        System.Windows.Forms.TreeViewEventArgs e)
{
    // Update the SelectedMenuId property on View Model
    _myViewModel.SelectedMenuId = int.Parse(e.Node.Tag);
}

p.s.

A Menu is a classic example of hierarchical data. So I have taken the liberty of stating the example based on a Menu model and the "SelectedMenuId" obviously refers to the id of the currently selected menu in the treeview.

Any other "views" (pun) and suggestions are highly appreciated.

Rajarshi
+1  A: 

You might find the WAF Windows Forms Adapter interesting. It shows how to apply the Model-View-ViewModel (MVVM) Pattern in a Windows Forms application. The Adapter implementation provides a solution for the missing Command support in Windows Forms.

jbe