Last active
August 29, 2015 14:11
-
-
Save EfrainReyes/88033d2f3aa3cff000e7 to your computer and use it in GitHub Desktop.
Using INotifyPropertyChanged without having to pass the property name or an expression. Running Example: https://dotnetfiddle.net/4P0dhM
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class CustomerModel : ModelBase { | |
private string _customerName; | |
public string CustomerName { | |
get { return _customerName; } | |
set { SetProperty(ref _customerName, value); } | |
} | |
private int _orderCount; | |
public int OrderCount { | |
get { return _orderCount; } | |
set { SetProperty(ref _orderCount, value); } | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.ComponentModel; | |
using System.Runtime.CompilerServices; | |
public abstract class ModelBase : INotifyPropertyChanged, IDisposable { | |
private event PropertyChangedEventHandler _propertyChanged; | |
private List<PropertyChangedEventHandler> _propertyChangedSubscribers = | |
new List<PropertyChangedEventHandler>(); | |
// this is Miguel Castro's technique from his Pluralsight course | |
// where he can add a delegate to an event handler | |
// without having to worry about duplicates | |
public event PropertyChangedEventHandler PropertyChanged { | |
add { | |
if (!_propertyChangedSubscribers.Contains(value)) { | |
_propertyChanged += value; | |
_propertyChangedSubscribers.Add(value); | |
} | |
} | |
remove { | |
_propertyChanged -= value; | |
_propertyChangedSubscribers.Remove(value); | |
} | |
} | |
private void NotifyPropertyChanged(string propertyName) | |
{ | |
if (_propertyChanged != null) | |
{ | |
_propertyChanged(this, new PropertyChangedEventArgs(propertyName)); | |
} | |
} | |
protected void SetProperty<T>(ref T field, T value, [CallerMemberName]string property = "") { | |
if (!EqualityComparer<T>.Default.Equals(field, value)) { | |
field = value; | |
NotifyPropertyChanged(property); | |
} | |
} | |
// my own little twist | |
// http://stackoverflow.com/q/1061727/2563028 | |
// this way instead of unregistering event handlers manually | |
// one can simply Dispose() the object | |
public void Dispose() { | |
_propertyChanged = null; | |
_propertyChangedSubscribers.Clear(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
public class Program | |
{ | |
public static void Main() | |
{ | |
using (var model = new CustomerModel()) { | |
model.PropertyChanged += (sender, e) => { | |
Console.WriteLine("{0}: {1}", e.PropertyName, sender.GetType().GetProperty(e.PropertyName).GetValue(sender)); | |
}; | |
model.CustomerName = "Efraín Reyes"; | |
model.OrderCount = 5; | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment