views:

120

answers:

1

I'm starting to use the MVVMLight framework and have a question about binding to properties in the ViewModel. I found that I have to call the RaisePropertyChanged method in the setter for the property in order for the View to be updated. And I have to call RaisePropertyChanged from through the dispatcher otherwise I get a thread access error.

    public string Lat { get { return _lat; } set
    {
        _lat = value;
        Deployment.Current.Dispatcher.BeginInvoke(() => RaisePropertyChanged("Lat"));
    } }

This works but its a lot of code to get auto binding properties. Is there a helper to handle this more cleanly?

+2  A: 

Raising PropertyChanged events is mandatory when you want to bind UI elements to properties on your model classes, independently of whether you're using MVVM Light or not. In fact it's easier with MVVM Light as it provides the RaisePropertyChanged method, which you would otherwise have to code yourself. :)

Using Dispatcher.BeginInvoke() is only needed if the set accessor of your property can be invoked from a thread different from the UI thread. Otherwise it's OK to call RaisePropertyChanged directly.

Andréas Saudemont
You can also save a LOT of time using Laurent's code snippets for creating properties (rather than create them yourself) as it wires up all the necessary code. You'll also want to start using the DispatcherHelper class instead of manually calling out to Deployment.Current.Dispatcher...
Chris Koenig