Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Diagnostics;
- using System.Linq.Expressions;
- namespace Core
- {
- #region Tests
- public class TestNotifyPropertyChangedBase
- {
- public static void Test()
- {
- MyNotifyClass myclass = new MyNotifyClass();
- myclass.PropertyChanged += (s, e) =>
- {
- Debug.WriteLine("-----Property " + e.PropertyName + " Changed");
- };
- myclass.FirstName = "Mary";
- myclass.LastName = "Smith";
- myclass.MiddleInitial = "B";
- Debug.WriteLine(myclass.DisplayName);
- //married, last name changed
- myclass.LastName = "Jones";
- Debug.WriteLine(myclass.DisplayName);
- }
- }
- public class MyNotifyClass : NotifyPropertyChangedBase
- {
- public MyNotifyClass()
- {
- //raises the PropertyChanged when any of the FirstName, Middle, or Last Name is changed
- UpdateOnProperty(() => DisplayName,
- () => FirstName, () => MiddleInitial, () => LastName);
- //update Initials when first or last name changes
- UpdateOnProperty(() => Initials,
- () => FirstName, () => LastName);
- RegisterUpdateAction(() => Initials, () =>
- {
- if (!string.IsNullOrEmpty(Initials))
- Debug.WriteLine("Initials Updated to " + Initials);
- });
- }
- string _firstName;
- public string FirstName
- {
- get { return _firstName; }
- set
- {
- if (_firstName == value)
- return;
- _firstName = value;
- OnPropertyChanged(() => FirstName);
- }
- }
- string _middleInitial;
- public string MiddleInitial
- {
- get { return _middleInitial; }
- set
- {
- if (_middleInitial == value)
- return;
- _middleInitial = value;
- OnPropertyChanged(() => MiddleInitial);
- }
- }
- string _lastName;
- public string LastName
- {
- get { return _lastName; }
- set
- {
- if (_lastName == value)
- return;
- _lastName = value;
- OnPropertyChanged(() => LastName);
- }
- }
- public string Initials
- {
- get
- {
- if (string.IsNullOrEmpty(FirstName) || string.IsNullOrEmpty(LastName))
- return null;
- if (FirstName.Length < 1 || LastName.Length < 1)
- return null;
- return string.Format("{0}{1}", FirstName.Substring(0, 1).ToUpper(), LastName.Substring(0, 1).ToUpper());
- }
- }
- public string DisplayName
- {
- get
- {
- return string.Format("{0} {1} {2}", FirstName, MiddleInitial, LastName);
- }
- }
- }
- #endregion
- /// <summary>
- /// Base class that implements <see cref="INotifyPropertyChanged"/>
- /// </summary>
- public abstract class NotifyPropertyChangedBase : INotifyPropertyChanged
- {
- readonly Dictionary<string, Action> registeredActions = new Dictionary<string, Action>();
- public event PropertyChangedEventHandler PropertyChanged;
- /// <summary>
- /// Raise the PropertyChanged event if actions are registrered
- /// </summary>
- protected virtual void CheckRegeristeredAction(string propertyName)
- {
- Action updateAction = null;
- if (registeredActions.TryGetValue(propertyName, out updateAction))
- {
- if (updateAction != null)
- updateAction();
- }
- }
- /*
- //Example
- this.UpdateOnProperty(() => Title,
- () => Name, () => MiddleInt, () => LastName);
- */
- /// <summary>
- /// Updates a property when one or more of the specified properties are changed
- /// </summary>
- protected void UpdateOnProperty<T>(Expression<Func<T>> updateExpression, params Expression<Func<T>>[] propertyExpressions)
- {
- Action updateAction = () => OnPropertyChanged(updateExpression);
- foreach (var expression in propertyExpressions)
- {
- MemberExpression memberExpression = (MemberExpression)expression.Body;
- string updatePropertyName = memberExpression.Member.Name;
- this.RegisterUpdateAction(expression, updateAction);
- }
- }
- /*
- //Example
- this.RegisterUpdateAction(() => Name, () =>
- {
- MessageBox.Show("Name Updated" + this.Name);
- });
- */
- /// <summary>
- /// Registers an action that is called when the provied property is changed
- /// </summary>
- protected void RegisterUpdateAction<T>(Expression<Func<T>> expression, Action updateAction)
- {
- string propertyName = GetPropertyName(expression);
- registeredActions[propertyName] = updateAction;
- }
- /// <summary>
- /// Fires the Notify Property Changed event
- /// </summary>
- protected void OnPropertyChanged<T>(Expression<Func<T>> expression)
- {
- string propertyName = GetPropertyName(expression);
- OnPropertyChanged(propertyName);
- }
- /// <summary>
- /// Fires the Notify Property Changed event
- /// </summary>
- protected void OnPropertyChanged(string propertyName)
- {
- PropertyChangedEventHandler handler = PropertyChanged;
- if (handler != null)
- {
- handler(this, new PropertyChangedEventArgs(propertyName));
- }
- CheckRegeristeredAction(propertyName);
- }
- /// <summary>
- /// Gets the string name for the property
- /// </summary>
- protected string GetPropertyName<T>(Expression<Func<T>> expression)
- {
- if (expression.NodeType != ExpressionType.Lambda)
- {
- throw new ArgumentException("Value must be a lamda expression", "expression");
- }
- if (!(expression.Body is MemberExpression))
- {
- throw new ArgumentException("The body of the expression must be a memberref", "expression");
- }
- MemberExpression memberExpression = (MemberExpression)expression.Body;
- return memberExpression.Member.Name;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment