question

PaulWPFDevelopment-2389 avatar image
0 Votes"
PaulWPFDevelopment-2389 asked PaulWPFDevelopment-2389 answered

WPF Model update in thread not propagating to ViewModel

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-wpfdotnet-wpf-xaml
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

PeterFleischer-3316 avatar image
0 Votes"
PeterFleischer-3316 answered PeterFleischer-3316 edited

Hi Paul,
try following demo: 198985-x.txt

Result:

199002-x.gif



x.txt (2.7 KiB)
x.gif (45.8 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

PaulWPFDevelopment-2389 avatar image
0 Votes"
PaulWPFDevelopment-2389 answered

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?

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.