Click here to Skip to main content
15,897,226 members
Articles / Programming Languages / C# 4.0

WPF Control Composition (Part 1 of 2)

Rate me:
Please Sign up or sign in to vote.
4.94/5 (9 votes)
16 Mar 2012CPOL7 min read 42.6K   2K   36  
Composing controls in a user control improves the consistency of a resulting application but does not add much extra work or complexity.
namespace WpfApplication1.ViewModel.Base
{
  using System;
  using System.ComponentModel;
  using System.Linq.Expressions;

  /// <summary>
  /// Tell bound controls (via WPF binding) to refresh their display.
  /// 
  /// via call to:
  /// 
  /// this.NotifyPropertyChanged(() => this.Name);
  /// 
  /// Advantage: The property name is verified at compiler run-time.
  /// </summary>
  public class BaseVM : INotifyPropertyChanged
  {
    #region INotifyPropertyChanged Members
    public event PropertyChangedEventHandler PropertyChanged;

    /// <summary>
    /// Tell bound controls (via WPF binding) to refresh their display.
    /// 
    /// Sample call: this.NotifyPropertyChanged(() => this.IsSelected);
    /// where 'this' is derived from <seealso cref="BaseVM"/>
    /// and IsSelected is a property.
    /// </summary>
    /// <typeparam name="TProperty"></typeparam>
    /// <param name="property"></param>
    public void NotifyPropertyChanged<TProperty>(Expression<Func<TProperty>> property)
    {
      var lambda = (LambdaExpression)property;
      MemberExpression memberExpression;

      if (lambda.Body is UnaryExpression)
      {
        var unaryExpression = (UnaryExpression)lambda.Body;
        memberExpression = (MemberExpression)unaryExpression.Operand;
      }
      else
        memberExpression = (MemberExpression)lambda.Body;

      this.OnPropertyChanged(memberExpression.Member.Name);
    }
    
    /// <summary>
    /// Tell bound controls (via WPF binding) to refresh their display.
    /// 
    /// Sample call: this.OnPropertyChanged("IsSelected");
    /// where 'this' is derived from <seealso cref="BaseVM"/>
    /// and IsSelected is a property.
    /// </summary>
    /// <param name="propertyName">Name of property to refresh</param>
    protected virtual void OnPropertyChanged(string propertyName)
    {
      if (this.PropertyChanged != null)
        this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
    #endregion INotifyPropertyChanged Members
  }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Germany Germany
The Windows Presentation Foundation (WPF) and C# are among my favorites and so I developed Edi

and a few other projects on GitHub. I am normally an algorithms and structure type but WPF has such interesting UI sides that I cannot help myself but get into it.

https://de.linkedin.com/in/dirkbahle

Comments and Discussions