Created
September 22, 2020 15:24
-
-
Save smailliwcs/68f8e0d4050df13ba120e17c5676980c to your computer and use it in GitHub Desktop.
Implementing INotifyPropertyChanged
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.Collections.Generic; | |
using System.ComponentModel; | |
using System.Runtime.CompilerServices; | |
public class ObservableObject : INotifyPropertyChanged | |
{ | |
public event PropertyChangedEventHandler PropertyChanged; | |
protected ObservableObject() { } | |
protected void OnPropertyChanged(PropertyChangedEventArgs e) | |
{ | |
PropertyChangedEventHandler handler = PropertyChanged; | |
if (handler == null) | |
{ | |
return; | |
} | |
handler(this, e); | |
} | |
protected void OnPropertyChanged(string propertyName) | |
{ | |
OnPropertyChanged(new PropertyChangedEventArgs(propertyName)); | |
} | |
protected bool Set<T>(ref T field, T value, [CallerMemberName] string propertyName = null) | |
{ | |
if (EqualityComparer<T>.Default.Equals(field, value)) | |
{ | |
return false; | |
} | |
field = value; | |
OnPropertyChanged(propertyName); | |
return true; | |
} | |
} |
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
<?xml version="1.0" encoding="utf-8"?> | |
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet"> | |
<CodeSnippet Format="1.0.0"> | |
<Header> | |
<Title>propn</Title> | |
</Header> | |
<Snippet> | |
<Declarations> | |
<Literal> | |
<ID>type</ID> | |
<Default>string</Default> | |
</Literal> | |
<Literal> | |
<ID>field</ID> | |
<Default>myProperty</Default> | |
</Literal> | |
<Literal> | |
<ID>property</ID> | |
<Default>MyProperty</Default> | |
</Literal> | |
</Declarations> | |
<Code Language="csharp"> | |
<![CDATA[private $type$ $field$; | |
public $type$ $property$ | |
{ | |
get { return $field$; } | |
set { Set(ref $field$, value); } | |
}]]> | |
</Code> | |
</Snippet> | |
</CodeSnippet> | |
</CodeSnippets> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment