How to disable or reset value inside its property changed
Hi I am developing mvvm wpf application , my problem is when user select some item in combobox I am asking a question to user yes/no, if user selects no i want to reset the value to开发者_开发知识库 previous selected item.. I am able to reset it and its value getting updated .. but the problem is the old value which i am setting is inside property changed event which is not getting updated in UI (Since I am doing it inside Property changed , i guess). Is there any work around or solution for this.
Make sure your VM implement INotifyPropertyChanged, and then make sure that the property whose old value you are resetting sends a notification when its value changes. Binding will then take care of updating the bound control.
public class ViewModel : INotifyPropertyChanged
{
public object PropertyToReset
{
get { return _propertyToReset; }
set
{
if (_propertyToReset == value) { return; }
// capture the old value in case the user decides not to change the value
_oldPropertyToResetValue = _propertyToReset;
_propertyToReset = value;
NotifyPropertyChanged("PropertyToReset");
}
}
public bool IsPropertyChanging
{
get { _isPropertyChanging; }
set
{
if (_isPropertyChanging == value) { return; }
_isPropertyChanging = value;
NotifyPropertyChanged("IsPropertyChanging");
if (value == false) { PropertyToReset = _oldPropertyToResetValue; }
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
精彩评论