I’m working on a WPF application using DevExpress, and also using the mvvm pattern, and have the following problem:
- I have a view model that has a boolean property (for instance IsChecked)
- I have a view that is a devexpress data grid that is bound to a collection of the above mentioned view model items
- A row of that devexpress data grid is of type check box, and it’s bound to the IsChecked property
- I have a data template for the row of devexpress grid for showing a line in the row, if IsChecked is true
All this works fine when I check/uncheck the check box column in the data grid…. The problem is when I change the model property’s value: the view does not change….
The model implements INotifyPropertyChanged.
It seems to be that DevExpress makes a wrapper for each item, and then does not notify the view when the model’s property has changed.
After search into the DevExpress’s Support Center, I could not find anything that helps me to solve this issues. But thanks to this nice application (Snoop) I get solve it.
The thing is that the DevExpress’s grid collection do not behave like the other WPF’s standard collections. In any WPF’s collection each row (or item in a collection) has in the
DataContextthe ViewModel (or any class of the collection type). The DevExpress’s grid do not works like that…Each
GridRowitem of the DevExpress’s grid has an object of typeRowDatain theDataContext, and each cell (are of typeGridCellContentPresenter) has an object of typeEditGridCellData. As we can see this objects are not the same type of our collection.So how we can do any two-way binding between our view model, and our view row or cell item in the grid?
If we are making a row template:
The object that is in the
DataContextin each row is of typeRowData. This type have a property namedDataContext, this property is of typeRowTypeDescriptor(like a wrapper of our collection type). This do not works for making our binding. BUT the typeRowDatahas a property namdeRowin which we can find our collection’s type row object, so the only thing that we have to do is to make a binding to this property. For instance, in this case we want that a line cross my row if a bool field is set to true:If we are making a cell template:
In this case is a bit more complex. because the object
EditGridCellDatahas not any property where we can find our collection’s object type. The only we can find here is a property calledDatain which we can find a object of typeRowTypeDescriptor, so do not work fine. We need to solve this using a ancestor type binding. See this example:Here we have a date control that will be bound to the model and will be correctly notified. Hope this could be useful for every one that had this problem. It is a real head ache.