Last active
December 12, 2017 08:58
-
-
Save aybarsyalcin/4469d957c6611bc6ff2ea90b1a2fd9a4 to your computer and use it in GitHub Desktop.
ViewModel Support - Snippet
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
namespace namespaces | |
{ | |
public class BaseViewModel : ObservableObject | |
{ | |
} | |
} |
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; | |
namespace namespaces | |
{ | |
/// <summary> | |
/// Observable object with INotifyPropertyChanged implemented | |
/// </summary> | |
public class ObservableObject : INotifyPropertyChanged | |
{ | |
/// <summary> | |
/// Sets the property. | |
/// </summary> | |
/// <returns><c>true</c>, if property was set, <c>false</c> otherwise.</returns> | |
/// <param name="backingStore">Backing store.</param> | |
/// <param name="value">Value.</param> | |
/// <param name="propertyName">Property name.</param> | |
/// <param name="onChanged">On changed.</param> | |
/// <typeparam name="T">The 1st type parameter.</typeparam> | |
protected bool SetProperty<T>( | |
ref T backingStore, T value, | |
[CallerMemberName]string propertyName = "", | |
Action onChanged = null) | |
{ | |
if (EqualityComparer<T>.Default.Equals(backingStore, value)) | |
return false; | |
backingStore = value; | |
onChanged?.Invoke(); | |
OnPropertyChanged(propertyName); | |
return true; | |
} | |
/// <summary> | |
/// Occurs when property changed. | |
/// </summary> | |
public event PropertyChangedEventHandler PropertyChanged; | |
/// <summary> | |
/// Raises the property changed event. | |
/// </summary> | |
/// <param name="propertyName">Property name.</param> | |
protected void OnPropertyChanged([CallerMemberName]string propertyName = "") | |
{ | |
var changed = PropertyChanged; | |
if (changed == null) | |
return; | |
changed.Invoke(this, new PropertyChangedEventArgs(propertyName)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment