Note that there are some explanatory texts on larger screens.

plurals
  1. POMVVM Datagrid updated from View and Model
    text
    copied!<p>I have a datagrid, which binds to an Item object which implements INotifyPropertyChanged.</p> <p>In the ViewModel I subscribe to changes from an external device service, which updates the Item object. The datagrid is editable, so Item can also be changed from the View. The value should be written to the device, but not yet updated in the view, as the device write might fail. If it succeeds, the device will issue an event, which I already have subscribed to.</p> <p>Some of my concerns are.</p> <p>Where do I call write on the device service, from the ViewModel or from the Item object? How to ensure the value displayed in the datagrid is "reverted" after editing, until receiving the event from the device?</p> <p>Some thoughts</p> <ol> <li><p>If it's the Item object, then the Item object is no longer a DTO, but rather a ViewModel I guess. So I'll have two view models for the same view (user control). One for the user control and one for the items in the datagrid. That doesn't fit my understanding of a view model. But perhaps it's wrong? And how does the Item then know if the value was updated from the view (by the user) or the view model (by the device service)?</p></li> <li><p>The ViewModel subscribes to PropertyChanged on the Item object. To detect if value is changed from View, the ViewModel can unsubscribe from PropertyChanged or setting a flag when getting events from the service. It seems clumpsy, but will work. Perhaps I should make two properties: ViewValue and ServiceValue. The ViewModel should update ServiceValue and subscribe to ViewValue, where it can revert ViewValue to ServiceValue after having read it.</p></li> <li><p>The View handles CellEditEnding and notifies the view model</p></li> </ol>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload