Esempio n. 1
0
        /// <summary>
        /// Set the property with the specified value. If the value is not equal with the field then the field is
        /// set, a PropertyChanged event is raised and it returns true.
        /// </summary>
        /// <typeparam name="T">Type of the property.</typeparam>
        /// <param name="field">Reference to the backing field of the property.</param>
        /// <param name="value">The new value for the property.</param>
        /// <param name="propertyName">The property name. This optional parameter can be skipped
        /// because the compiler is able to create it automatically.</param>
        /// <returns>True if the value has changed, false if the old and new value were equal.</returns>
        protected bool SetProperty <T>(ref T field, T value, [CallerMemberName] string propertyName = null)
        {
            //compare to the old value
            if (object.Equals(field, value))
            {
                return(false);
            }

            //get fields old value
            var oldValue = field;

            //set field to new value
            field = value;

            //create new arguments
            var args = new PropertyChangedEventArgsEx(propertyName, value, oldValue);

            //raise event
            RaisePropertyChanged(args);

            //inform subclasses
            OnPropertyChanged(this, args);

            //return true since property was changed
            return(true);
        }
Esempio n. 2
0
        /// <summary>
        /// Raises PropertyChanged event.
        /// </summary>
        /// <param name="sender">Event sender.</param>
        /// <param name="args">Event arguments.</param>
        protected void RaisePropertyChanged(object sender, PropertyChangedEventArgsEx args)
        {
            if (sender == null)
            {
                throw new ArgumentNullException(nameof(sender));
            }

            if (args == null)
            {
                throw new ArgumentNullException(nameof(args));
            }

            //process event in derived classes
            if (OnPropertyChanging(sender, args))
            {
                PropertyChanged?.Invoke(sender, args);
            }
        }
Esempio n. 3
0
 /// <summary>
 /// This method is called when property changed and we aboout to raise PropertyChanged event.
 /// </summary>
 /// <returns>
 /// True if PropertyChanged should be raised otherwise false.
 /// </returns>
 /// <param name="sender">Event sender.</param>
 /// <param name="args">Event arguments.</param>
 protected virtual bool OnPropertyChanging(object sender, PropertyChangedEventArgsEx args)
 {
     return(true);
 }
Esempio n. 4
0
 /// <summary>
 /// Raises PropertyChanged event.
 /// </summary>
 /// <param name="args">PropertyChangedEvent aruments.</param>
 protected void RaisePropertyChanged(PropertyChangedEventArgsEx args)
 {
     //Null or empty string propertyName should be allowed http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged.propertychanged.aspx
     RaisePropertyChanged(this, args);
 }
Esempio n. 5
0
 /// <summary>
 /// This method is called once property has changed and processed.
 /// </summary>
 /// <param name="sender">Event sender.</param>
 /// <param name="args">Event arguments.</param>
 protected virtual void OnPropertyChanged(object sender, PropertyChangedEventArgsEx args)
 {
 }