Model-View-ViewModel (MVVM) and Windows Forms

Coordinator
Sep 25, 2010 at 10:02 AM

I read often in the MVVM community that the Model-View-ViewModel Pattern works only with WPF/Silverlight applications. That’s not true.

MVVM is a design pattern. Per definition a design pattern shows a common solution in the object-oriented world. Such solutions can be applied in various platforms (WPF, WinForms, Java Swing, etc.). I agree that MVVM is best used with WPF/Silverlight because it leverages the strong data binding capabilities. However, Windows Forms supports data binding as well.

The WAF Windows Forms Adapter shows how to apply the Model-View-ViewModel Pattern in a Windows Forms application. It ships the same BookLibrary sample application as the WPF Application Framework (WAF) download. It just extends the sample application with an alternative Presentation layer implementation which uses Windows Forms instead of WPF.

This BookLibrary sample application proves that the Applications and Domain layer of a WAF-based application is really independent of WPF. This way it was possible to replace the WPF technology with Windows Forms by writing a new Presentation layer whereas the code of the lower layers aren’t even touched. As a result the Windows Forms Views uses the same ViewModel classes as the WPF Views does. That’s how a clean separation into distinct layers should be.

jbe
 

Feb 14, 2011 at 1:07 AM
Edited Feb 25, 2011 at 5:48 PM

EDIT: Nevermind.  It doesn't really matter.

Mar 2, 2011 at 5:39 PM
Edited Mar 2, 2011 at 5:53 PM

Is there any chance to have the WAF Windows Forms Adapter library extended with adapters for key event handling (already asked this in http://waf.codeplex.com/discussions/248166, sorry for the crosspost) and, while I'm at it, for attaching the WinForms ErrorProvider to INotifyDataErrorInfo?

Regards,
DrJ

Coordinator
Mar 4, 2011 at 6:26 PM

Hi DrJ,

1. You could handle the Key – Events in Windows Forms and execute the commands manually:

private void FirstnameBoxKeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Enter)
    {
        ((PersonViewModel)DataContext).SomeCommand.Execute(null);
    }
}

2. The interface INotifyDataErrorInfo is only available for Silverlight at the moment. But the ErrorInfoProvider supports the IDataErrorInfo interface as it is shown in the BookLibrary.WinForms sample application.

jbe

Feb 9, 2012 at 5:22 PM

Hi everybody,

I am Pedro, I'm studing in Computer Engineering UTAD (Portugal), and within of work proposed in a course Programming Methodology III, I would like to participate in this forum discussion.