0

I am using MVVM in my WPF4 application.

In my application, the ViewModel has an ObservableCollection ProductsList property, an ICommand Save command property. The Product class has two property: ProductName and Price.

In the UI, the window's DataContext binds to the ViewModel, the xamDataGrid's DataSource binds to the ProductsList property, and a button binds to the Save property.

A user will make change to the Price column in the view.

What I try to do is when the application first fire up, the SAVE button is disabled, since there's nothing new to be saved. As soon as the user makes a change to any record, like changing the price of a Product, the SAVE button will be enabled.

My question is: does xamDataGrid have a property that indicates the bidden data has been changed (so the data grid is dirty)? If there's a such property, I would like to bind this to a property of the ViewModel, for example, bool DataChanged, then I would use this DataChanged property to set the CanExcuteMethod of the "Save" command, to enable the SAVE button.

Or, is there any other way to accomplished this function?

Thank you.

Ying
  • 1
  • 1

1 Answers1

0

The XamDataGrid doesn't expose a property for this as this shouldn't be tracked by the UI control since it isn't aware of the data store or if the data changes from another source other than itself.

The best approach is to track this in the ViewModel because if you rely on the control in the View then the ViewModel has a dependency on the View which may prevent you from using a different View with the same VeiwModel.

To do this, create the property in your ViewModel like you would if you were going to bind the grid to it and set it's value when the data in the ViewModel changes.

alhalama
  • 3,218
  • 1
  • 15
  • 21