Home > Software engineering >  Is it possible to initialize a property change value?
Is it possible to initialize a property change value?

Time:10-05

I'm newer to MVVM design pattern , I have a class User and a property IsEnabled,

I will use this class later in a ViewModel , what I would like to know if is it possible to initialize a property change in this class ( As an example Set IsEnabled to True )

 public  class User : INotifyPropertyChanged
    {
        public int UserId { get; set; }
        public string UserName { get; set; }
 public event PropertyChangedEventHandler PropertyChanged;

        private bool isEnabled;
        public bool IsEnabled
        {
            get { return isEnabled; }
            set
            {
                if (isEnabled != value)
                {
                    isEnabled = value;
                    OnPropertyChanged("IsEnabled");
                }
            }
        }
        protected void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null) 
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

The class compiles without any errors :

enter image description here

CodePudding user response:

Change the declaration of your backing property:

private bool isEnabled = true;

Then the initial value as applied to the DataContext and thus to any Binding extensions will propagate the true value.

  • Related