// (c) Copyright Microsoft Corporation. // This source is subject to the Microsoft Public License (Ms-PL). // Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details. // All other rights reserved. using System; using System.Collections.Generic; using System.Windows.Threading; namespace System.ComponentModel { /// /// A base class for data objects that implement the property changed /// interface, offering data binding and change notifications. /// public class PropertyChangedBase : INotifyPropertyChanged { /// /// A static set of argument instances, one per property name. /// private static Dictionary _argumentInstances = new Dictionary(); /// /// The property changed event. /// public event PropertyChangedEventHandler PropertyChanged; /// /// Notify any listeners that the property value has changed. /// /// The property name. protected void NotifyPropertyChanged(string propertyName) { if (string.IsNullOrEmpty(propertyName)) { throw new ArgumentException("PropertyName cannot be empty or null."); } PropertyChangedEventHandler handler = PropertyChanged; if (handler != null) { PropertyChangedEventArgs args; if (!_argumentInstances.TryGetValue(propertyName, out args)) { args = new PropertyChangedEventArgs(propertyName); _argumentInstances[propertyName] = args; } // Fire the change event. The smart dispatcher will directly // invoke the handler if this change happened on the UI thread, // otherwise it is sent to the proper dispatcher. SmartDispatcher.BeginInvoke(delegate { handler(this, args); }); } } } }