MVVM and Validation at MIX11

I’m happy to announce that yesterday I had a presentation at MIX’11. I was talking about effective validation techniques with MVVM in Silverlight 4.0. In the session we discussed the different aspects of validation like designing the user experience and the architecture. The video of the session is now available for streaming on Channel9.

Also, I made a promise that I would post the demo code. Which I’m doing right now. So if you want to take a look at the ideas I implemented for the demo, I encourage you to do so.

However please note that this is only demo code… It is NOT tested and I advise you not to use it in production environment. It’s supposed to demonstrate a couple of ideas.

However if you are interested in having a fully functioning and tested version please just say so and I’ll release a somewhat updated and tested (in-production used) codebase.

Here is the demo code:

Mix11 Demo Source code


24 thoughts on “MVVM and Validation at MIX11

  1. Demo was great. You answered most of my common day to day question.Interesting to watch.
    We want to implement following validation can give me some direction .
    After the complex validation or a remote validation if there is an error we want set the focus back to the control to the orignal control.
    Other question is how do we reject the changes if the user entered invalid info and setting back to the orginal value. Example if the user if the departure date is < arriaval date setting the orginal value but show the error message.


    • Hi Senthil!

      Well there is quite a fast fix for that… If you use the INotifyDataErrorInfo interface you could write a behavior to subscribe for it… The errorschanged event will be fired when there is something worong with the bound property… All you have to do is to call focus on the associatedobject… This is nice because it is kind of ui logic…so writing a behavior for it kinda makes sense 🙂 also if you want control over focus in your viewmodel (this shuld be avoided) than you should implement a focusmanager…. If you need more info on that get back to me…

      On property level changetracking…. Yeah… IEditableObject is no much of a help here… In this case you have to know which properties changed… Basically in a way you gotta log the edits… Now there are interfaces in silverlight that could be used for changetracking… But i think they won’t turn out to be usefull in this case… I advise you to implement this in a baseclass…. However in the child class you will always have to indicate in the setter that a property is change…i would use the intofiypropertychanged interface for that… So you just kind of need an object to store original propertyvalues, than whenever e porpertychange event fires you store them…. Then on validation errors you get the original value and reset it… It will need some plumbing code to do that…. If you need anymore help get back to me 🙂

  2. I really enjoyed your demo. At the end you were asked a question about initial validation.

    Lets say you create a new record for a datagrid and then set focus to the record. When the record gets focus all of the validation fires even though the user hasn’t had a chance to enter anything. While the user can go in and enter the data it’s bad UX. Can you expand on your explanation for how to handle this elegantly>

    Thank you

    • Hi Matt!

      First, I’m really happy you loved the session! (also please rate it on ch9, if you wanna see more stuff like that :D) Thnx!

      I have some good news for you (again :D).
      Well the problem is that DataGrid handles these attributes by itself, which is kind of okay, but the problem is that it validates on beginedit… you can’t change the behavior of that (at least there is no way to do that that i know of…)
      Here comes the good part… there is a cool interface / collection that supports this kind of scenarios… In my presentation i didn’t have time to work with lists so this was not an issue…
      Now what you need is the IEditableCollectionView (my advice on MVVM: always pormote your data (=lists) to the UI through a CollectionView. Now this Interface has a method called AddNew()… this is all you need… you get the new item in the collection, and you also get the focus… and there is no validation running just yet!… on EndEdit the Datagrid will run validation! So all you need is now is an implementation… which you have: Meet the PagedCollectionView class! You’ll love it 🙂

      If you need any help with the implementation, just get back to me, Hope this helps a lot!

      • I also have to mention, that after a cell loses focus it will be validated according to the attribute!

  3. This is a very good demo Zoltan. I really liked it. I would be really interested in having a fully functioning and tested version of the code

    • Hi Iain,

      Realy glad, you liked it… would be grateful if you could rate it to (if you want to see things like this next time :D)

      About the tested version: okay, I can’t give you a timeline right now, but sure… I will upload my own mvvm framework with validation. There will be a post about it!

  4. Great idea with behavior to support format exceptions.

    I have one question, what if user don’t enter any data in form and he can click next (I comment out default data in viewmodel) ?

    • Hi Michal!

      This is where object validation comes into play… On next (submit, ect) you shoukd valudate the whole object and indicate the results… Proprety level valdiation doesn’t really work here…

  5. I appreciate the work you’ve done for validation here. I’ve been looking for ways to validate objects with MVVM Light and I’m glad that you decided to extend this framework. I’m also interested in production quality codebase. Keep up the good work!

  6. Hi Zoltan,

    Thanks for your brillant presentation.

    I have a question regarding property validation.

    How do I make a button enabled on the same form only when all properties have been validated?

    Do I need to check each property for errors? how? or is there a better way of doing it?

    Can you please provide some sample code?



    • As I remember, ValidationViewModel base has a property HasError so you can extend this property (use RaisePropertyChanged) and Bind Button.IsEnabled to this property.

    • Hi Claudio! Production code is not yet available… I’m working on a framework that includes a lot of things besides validation I don’t have a timeframe yet, but I’ll post it here as soos as it is available

      Property validation is not enough for you, you need object validation! (When you need to know that all of your properties are valid!)

  7. Hi,

    In my previous email, I forgot to ask about the fully functioning version of your validation class.

    Where can I donwload it?

    This was your comment:

    “However if you are interested in having a fully functioning and tested version please just say so and I’ll release a somewhat updated and tested (in-production used) codebase.



  8. Very cool demo + presentation!!

    I have the same question that was asked, but I don’t think received a full answer or at least i did not understand it completely. We are using WCF services – so the ViewModel often wraps other entities. In these cases the model itself may contain the validation (or at least we have no access to the properties to inject code, etc.).

    How do you recommend we validate in these cases? Currently we can only validate on submit/save and have custom validation controls bind to custom ValidationResults (on view model), but the INotifyDataErrorInfo looks much more interesting, please advise.

    • If you bind your entities directly to the ui, then you can use data annotation, but you should still use validation implemented in the viewmodel on submit/save…. If you wrap in inside a viewmodel, then my approach works, so I don’t really get your situation… can you provide me with a sample?

  9. Zoltan. Thanks for your hard work. It really got me deep into thinking about validation.
    I have two related questions.

    The first is I noticed that you state “not to use it in production environment. It’s supposed to demonstrate a couple of ideas.” I understand your disclaimer. Are you aware of any tested source code, such as from CodePlex, Josh Smith or Karl Shifflett that you can recommend?

    Second question: I have to build my app in WPF. Right away I find that WPF FrameworkElement does not have public event EventHandler BindingValidationError; This feels like a dead end in the very beginning for WPF. Do you have any suggestion how I can get your ideas working in WPF?

    Thanks, Joe

  10. Hi Zoltan,

    Thank you very much for your great demo. What is the difference with a fully functionning and fully tested version? It looks quite good already 🙂

    How do we specify which project (V1, V2, …) should be run?

    Thank you very much for your help!


  11. Hello Zoltan,

    Thanks for the demo!

    It seems the link to the source code doesn’t work now. Could you please give a working link if possible?

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s