locked
implementing of MVVM RRS feed

  • Question

  • hi,

    Is it mandatory that we should not have code in code-behind file in WPF while implementing MVVM.

    Wednesday, August 21, 2013 11:27 AM

Answers

  • Putting any view logic in the code-behind of a view doesn't break the MVVM pattern. However, as one of the main reasons for using MVVM is separation of concerns, i.e. separate logic from presentation, you should keep any presentation/application logic in the view model and any business logic in the model.

    A good rule of thumb is that you should be able to unit test your application logic by executing the code in the view model without the view.

    Ideally the code-behind of a view contains only a constructor that calls the InitializeComponent method but it may also contain some additional code to control or interact with the view layer that is difficult or inefficient to express in XAML, such as for example complex animations.

    • Proposed as answer by Horizon_Net Wednesday, August 21, 2013 4:36 PM
    • Marked as answer by Leo (Apple) Yang Wednesday, August 28, 2013 12:49 PM
    Wednesday, August 21, 2013 12:59 PM

All replies

  • Putting any view logic in the code-behind of a view doesn't break the MVVM pattern. However, as one of the main reasons for using MVVM is separation of concerns, i.e. separate logic from presentation, you should keep any presentation/application logic in the view model and any business logic in the model.

    A good rule of thumb is that you should be able to unit test your application logic by executing the code in the view model without the view.

    Ideally the code-behind of a view contains only a constructor that calls the InitializeComponent method but it may also contain some additional code to control or interact with the view layer that is difficult or inefficient to express in XAML, such as for example complex animations.

    • Proposed as answer by Horizon_Net Wednesday, August 21, 2013 4:36 PM
    • Marked as answer by Leo (Apple) Yang Wednesday, August 28, 2013 12:49 PM
    Wednesday, August 21, 2013 12:59 PM
  • thanks you very much for your patience in replying with detail..

    i am new to MVVM pattern hence confused a lot, previously i have worked in WinForm and Moved to WPF

    one question:

    as of now what i have understood is that with out Impelementaion of MVVM in Wpf it's again same as Winform only except UI look and feel and using of XAML most and style of coding in code behind file  is similar to WinForms right?

    thanks,

    Bhagya

    Friday, August 23, 2013 9:08 AM
  • While WPF makes it easy to build applications using the MVVM pattern, you still can use code-behind in a WPF application just like you can in a WinForms application. Some of the most important differences between WPF and WinForms are:

    - WPF provides a cleaner separation of UI and 'code-behind'. Designers can allegedly design the UI while the developers develop the application and business logic.

    - WPF provides improved graphics rendering

    - WPF provides a much more flexible UI model where you can have UI elements inside other UI elements since it's all just XAML. WinForms requires you to build those types of controls and is much more limiting.

    For more information about the MVVM pattern and how it applies to WPF, please refer to this article written by Josh Smith: http://msdn.microsoft.com/en-us/magazine/dd419663.aspx

    Friday, August 23, 2013 10:45 AM