I use MVVM (prism) to develop a wpf application.
One of my model classes "StandardContact" has its own properties that are directly related to the view. I use IDataErrorInfo to track and report an error in the model. If there are any errors in the Model, I will disable the "Save" command.
When a user enters some data, I use the StandardContact.PropertyChanged handler to check if the Save command can be executed (that is, if the model data is entered by the user). The problem is that the StandardContact.PropertyChanged handler is called before the IDataErrorInfo verification code, so CanExecute for the Save command incorrectly reflects whether the command can be executed or not. What I'm looking for is that prior to executing CanExecute, IDataErrorInfo authentication should be done so that CanExecute requests the latest data in the model and decides whether it is enabled or not. Here is an example of the code I'm using
Model:
public class StandardContact :EntityBase, IDataErrorInfo
{
public virtual string Name
{
get { return _name; }
set { SetField(ref _name, value, () => Name); }
}
public string this[string propertyName]
{
get
{
string error = null;
}
ViewModel
public class SContactEditViewModel : NotificationObject, INavigationAware
{
StandardContact.PropertyChanged +=
new PropertyChangedEventHandler(StandardContact_PropertyChanged);
void StandardContact_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
SaveNewCommand.RaiseCanExecuteChanged();
}
}
source
share