I have I problem with a RadGridView. ..
I'm using the MVVM pattern and each row in the gridview is bound to its own ViewModel. At a certain point in the execution of the program I save a row to the database (this is done in the ViewModel of the row). This might have the consequence that some property in the ViewModel of the row is updated. Therefore I trigger the PropertyChanged event without specifying a property name, which will tell the row in the GUI to read the values of all the properties it is bound to.
My problem is that sometimes when I do this, the gridview tries to set the old values of the properties back to the ViewModel. Let's say I have a column bound to a property "Age" that has the value 34; then it will set the value of the property to 34. Normally, this is not a problem (since in the end the property will still have the same value). However, my rows can have one of two states, and in one of those states some of the cells/properties are not allowed to change. I enforce this in the GUI so that it is impossible to change a value of a cell/property that is not allowed to change. But to really make sure this never happens (in case some developer makes a mistake in the GUI), I throw an exception in the ViewModel if you try:
public
int
Age
{
get
{
return
_age; }
set
{
if
(IsAllowedToChangeAge)
{
if
(value != _age)
{
_age = value;
OnPropertyChanged(
"Age"
);
}
}
else
{
//The GUI should make sure this never happens.
throw
new
Exception(
"Cannot change age if..."
);
}
}
}
This works fine in normal cases because the GUI makes sure it never happens. In the example above, the user won't be able to edit the cell bound to Age if the IsAllowedToChangeAge property is false.
BUT as mentioned, sometimes when the ViewModel tells the gridview to update (through the PropertyChanged event), the gridview itself tries to set the values of the grid (to the same values as it had). When this happens and and the row is in a state that doesn't permit the values to change, the exception will be thrown!
How can I get around this? Why does the gridview set the same values as it already has? Can I stop this from happening? I have not found a pattern for when it happens...
I could get around this by not throwing an exception, but I'd really like to keep it to catch any developer mistakes.
Thanks // David