WPF Model update in thread not propagating to ViewModel

Paul WPF Development 41 Reputation points
2022-05-04T12:53:05.44+00:00

Developing a WPF application using the MVVM pattern.

The MVVM design is pretty straightforward and working with one caveat. The model is updated in a different thread from the UI and any updates in the model that occur on that thread do not get propagated to the UI. The internal dat in the model is correct but the binding doesn't appear to work when the model is updated in the non UI thread. Even when, in the UI thread, the update to the model is run using Dispatched.BeginInvoke.

The model is implemented as an object, this object is updated in a separate thread from the View Model. When the view model instantiated it spins off a thread and passes in a reference to the Model object, pretty straightforward design.

The thread receives data from a serial port and based on that data will modify the Model object. In the thread the model is updated correctly but the change is never propagated to the view model ui. I have verified in the debugger that the model object is correct but still no update ui.

My ViewModel code looks like this

class viewmodel : ViewModelBase
{
private readonly model = new model();

Boolean IsLevel => model.IsLevel; // this should bind the property of the model to the VM

}

class model : ViewModelBase
{
private Boolean _isLevel;
public Boolean IsLevel
{
get { return _isLevel; }
set
{
if (IsLevel != value )
{
{Dispatcher.BeginInvoke(() =>
{
_isLevel = value;
RaisePropertyChanged();
}
}

I have modified the model to call RaisePropertyChanged, I have modified the model to invoke the RaisePropertyChanged in the Dispatcher so the modification to the Model object occurs on the UI thread, no luck.

I have modified the View Model to call RaisePropertyChanged after the value is set and that properly updates the UI. Other fields in the View Model for the same object that are changed in the View Model directly update properly.

First question, is it possible to update an model object in a different thread and have the model to view model binding work? Are there any real examples of this? I have read numerous articles that say this is possible and have tried many many things but nothing works.

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,663 questions
XAML
XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
760 questions
0 comments No comments
{count} votes

1 additional answer

Sort by: Most helpful
  1. Paul WPF Development 41 Reputation points
    2022-05-05T09:30:37.03+00:00

    Thanks Peter, the following line seems to have done the trick.

    model.PropertyChanged += (s, e) => OnPropertyChanged(e.PropertyName);

    Does adding that line propagate the property change to the ViewModel? and will Object in the Model require the same update?

    0 comments No comments