WPF page refresh

WPF page refresh:

In WPF projects, class attributes have changed, but the value of the interface has not changed, and we do not have the class attribute set to do the processing of PropertyChanged.

How to achieve it?

First, we need to implement a INotifyPropertyChanged.

And implement a method:

public void OnPropertyChanged(string propertyName = null)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}

After such a modification, the use OnPropertyChanged (null); to.

I is defined as follows:

public class BaseViewModel: INotifyPropertyChanged
    {

        public event PropertyChangedEventHandler PropertyChanged;

        protected bool SetProperty<T>(ref T properValue, T newValue, string properName = null)
        {
            if (object.Equals(properValue, newValue))
                return false;
            properValue = newValue;
            OnPropertyChanged(properName);
            return true;
        }
        public void OnPropertyChanged(string propertyName = null)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }

    }

Above do a base class, data class inherits from it.

public class MyDataModel : BaseViewModel

when using it:

MyDataModel.value="Test";

MyDataModel.OnPropertyChanged(null);

It can be.

 

Guess you like

Origin www.cnblogs.com/youmeetmehere/p/10978526.html