Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 459737
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T22:48:10+00:00 2026-05-12T22:48:10+00:00

I am currently working with the Microsoft MVVM template and find the lack of

  • 0

I am currently working with the Microsoft MVVM template and find the lack of detailed examples frustrating. The included ContactBook example shows very little Command handling and the only other example I’ve found is from an MSDN Magazine article where the concepts are similar but uses a slightly different approach and still lack in any complexity. Are there any decent MVVM examples that at least show basic CRUD operations and dialog/content switching?


Everyone’s suggestions were really useful and I will start compiling a list of good resources

Frameworks/Templates

  • WPF Model-View-ViewModel Toolkit
  • MVVM Light Toolkit
  • Prism
  • Caliburn
  • Cinch

Useful Articles

  • WPF Apps With The Model-View-ViewModel Design Pattern
  • Data Validation in .NET 3.5
  • Using a ViewModel to Provide Meaningful Validation Error Messages
  • Action based ViewModel and Model validation
  • Dialogs
  • Command Bindings in MVVM
  • More than just MVC for WPF
  • MVVM + Mediator Example
    Application

Screencasts

  • Jason Dolinger on Model-View-ViewModel

Additional Libraries

  • WPF Disciples’ improved Mediator Pattern implementation(I highly recommend this for applications that have more complex navigation)
  • MVVM Light Toolkit Messenger
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T22:48:10+00:00Added an answer on May 12, 2026 at 10:48 pm

    Unfortunately there is no one great MVVM example app that does everything, and there are a lot of different approaches to doing things. First, you might want to get familiar with one of the app frameworks out there (Prism is a decent choice), because they provide you with convenient tools like dependency injection, commanding, event aggregation, etc to easily try out different patterns that suit you.

    The prism release:

    http://www.codeplex.com/CompositeWPF

    It includes a pretty decent example app (the stock trader) along with a lot of smaller examples and how to’s. At the very least it’s a good demonstration of several common sub-patterns people use to make MVVM actually work. They have examples for both CRUD and dialogs, I believe.

    Prism isn’t necessarily for every project, but it’s a good thing to get familiar with.

    CRUD:
    This part is pretty easy, WPF two way bindings make it really easy to edit most data. The real trick is to provide a model that makes it easy to set up the UI. At the very least you want to make sure that your ViewModel (or business object) implements INotifyPropertyChanged to support binding and you can bind properties straight to UI controls, but you may also want to implement IDataErrorInfo for validation. Typically, if you use some sort of an ORM solution setting up CRUD is a snap.

    This article demonstrates simple crud operations:
    http://dotnetslackers.com/articles/wpf/WPFDataBindingWithLINQ.aspx

    It is built on LinqToSql, but that is irrelevant to the example – all that is important is that your business objects implement INotifyPropertyChanged (which classes generated by LinqToSql do). MVVM is not the point of that example, but I don’t think it matters in this case.

    This article demonstrates data validation

    http://blogs.msdn.com/wpfsdk/archive/2007/10/02/data-validation-in-3-5.aspx

    Again, most ORM solutions generate classes that already implement IDataErrorInfo and typically provide a mechanism to make it easy to add custom validation rules.

    Most of the time you can take an object(model) created by some ORM and wrap it in a ViewModel that holds it and commands for save/delete – and you’re ready to bind UI straight to the model’s properties.

    The view would look like something like this (ViewModel has a property Item that holds the model, like a class created in the ORM):

    <StackPanel>
       <StackPanel DataContext=Item>
          <TextBox Text="{Binding FirstName, Mode=TwoWay, ValidatesOnDataErrors=True}" />
          <TextBox Text="{Binding LastName, Mode=TwoWay, ValidatesOnDataErrors=True}" />
       </StackPanel>
       <Button Command="{Binding SaveCommand}" />
       <Button Command="{Binding CancelCommand}" />
    </StackPanel>
    

    Dialogs:
    Dialogs and MVVM are a bit tricky. I prefer to use a flavor of the Mediator approach with dialogs, you can read a little more about it in this StackOverflow question:

    WPF MVVM dialog example

    My usual approach, which is not quite classic MVVM, can be summarized as follows:

    A base class for a dialog ViewModel that exposes commands for commit and cancel actions, an event to lets the view know that a dialog is ready to be closed, and whatever else you will need in all of your dialogs.

    A generic view for your dialog – this can be a window, or a custom “modal” overlay type control. At its heart it is a content presenter that we dump the viewmodel into, and it handles the wiring for closing the window – for example on data context change you can check if the new ViewModel is inherited from your base class, and if it is, subscribe to the relevant close event (the handler will assign the dialog result). If you provide alternative universal close functionality (the X button, for instance), you should make sure to run the relevant close command on the ViewModel as well.

    Somewhere you need to provide data templates for your ViewModels, they can be very simple especially since you probably have a view for each dialog encapsulated in a separate control. The default data template for a ViewModel would then look something like this:

    <DataTemplate DataType="{x:Type vmodels:AddressEditViewModel}">
       <views:AddressEditView DataContext="{Binding}" />
    </DataTemplate>
    

    The dialog view needs to have access to these, because otherwise it won’t know how to show the ViewModel, aside from the shared dialog UI its contents are basically this:

    <ContentControl Content="{Binding}" />
    

    The implicit data template will map the view to the model, but who launches it?

    This is the not-so-mvvm part. One way to do it is to use a global event. What I think is a better thing to do is to use an event aggregator type setup, provided through dependency injection – this way the event is global to a container, not the whole app. Prism uses the unity framework for container semantics and dependency injection, and overall I like Unity quite a bit.

    Usually, it makes sense for the root window to subscribe to this event – it can open the dialog and set its data context to the ViewModel that gets passed in with a raised event.

    Setting this up in this way lets ViewModels ask the application to open a dialog and respond to user actions there without knowing anything about the UI so for the most part the MVVM-ness remains complete.

    There are times, however, where the UI has to raise the dialogs, which can make things a bit trickier. Consider for example, if the dialog position depends on the location of the button that opens it. In this case you need to have some UI specific info when you request a dialog open. I generally create a separate class that holds a ViewModel and some relevant UI info. Unfortunately some coupling seems unavoidable there.

    Pseudo code of a button handler that raises a dialog which needs element position data:

    ButtonClickHandler(sender, args){
        var vm = DataContext as ISomeDialogProvider; // check for null
        var ui_vm = new ViewModelContainer();
        // assign margin, width, or anything else that your custom dialog might require
        ...
        ui_vm.ViewModel = vm.SomeDialogViewModel; // or .GetSomeDialogViewModel()
        // raise the dialog show event
    }
    

    The dialog view will bind to position data, and pass the contained ViewModel to the inner ContentControl. The ViewModel itself still doesn’t know anything about the UI.

    In general I don’t make use of the DialogResult return property of the ShowDialog() method or expect the thread to block until the dialog is closed. A non-standard modal dialog doesn’t always work like that, and in a composite environment you often don’t really want an event handler to block like that anyhow. I prefer to let the ViewModels deal with this – the creator of a ViewModel can subscribe to its relevant events, set commit/cancel methods, etc, so there is no need to rely on this UI mechanism.

    So instead of this flow:

    // in code behind
    var result = somedialog.ShowDialog();
    if (result == ...
    

    I use:

    // in view model
    var vm = new SomeDialogViewModel(); // child view model
    vm.CommitAction = delegate { this.DoSomething(vm); } // what happens on commit 
    vm.CancelAction = delegate { this.DoNothing(vm); } // what happens on cancel/close (optional)
    // raise dialog request event on the container
    

    I prefer it this way because most of my dialogs are non-blocking pseudo-modal controls and doing it this way seems more straightforward than working around it. Easy to unit test as well.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

currently I'm working on this ftp transfer. http://msdn.microsoft.com/en-us/library/ms229715.aspx I have setup my server computers
I'm currently using Microsoft Sync Framework and everything is working fine except the SQL
I am currently converting a very old, but working classic ASP site to ASP.Net.
I'm currently working on my own memory leak tracking system. I'm using Microsoft Visual
I'm currently working my way through the Microsoft SQL Server 2008 - Database Development
I am currently working on a very large legacy MFC MDI application. It has
I'm currently working with Microsoft SSIS and SQL server 2008 and I would like
I am currently working on an application which references the Microsoft Business Contact Manager
I am currently working on a project to perform disk defragmentation in Microsoft Windows
I am currently working on a system in Microsoft Access 2007, and I need

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.