Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The <code>INotifyPropertyChanged</code> interface <strong>is</strong> implemented with events. The interface has just one member, <code>PropertyChanged</code>, which is an event that consumers can subscribe to.</p> <p>The version that Richard posted is not safe. Here is how to safely implement this interface:</p> <pre><code>public class MyClass : INotifyPropertyChanged { private string imageFullPath; protected void OnPropertyChanged(PropertyChangedEventArgs e) { PropertyChangedEventHandler handler = PropertyChanged; if (handler != null) handler(this, e); } protected void OnPropertyChanged(string propertyName) { OnPropertyChanged(new PropertyChangedEventArgs(propertyName)); } public string ImageFullPath { get { return imageFullPath; } set { if (value != imageFullPath) { imageFullPath = value; OnPropertyChanged("ImageFullPath"); } } } public event PropertyChangedEventHandler PropertyChanged; } </code></pre> <p>Note that this does the following things:</p> <ul> <li><p>Abstracts the property-change notification methods so you can easily apply this to other properties;</p></li> <li><p>Makes a copy of the <code>PropertyChanged</code> delegate <strong>before</strong> attempting to invoke it (failing to do this will create a race condition).</p></li> <li><p>Correctly implements the <code>INotifyPropertyChanged</code> interface.</p></li> </ul> <p>If you want to <em>additionally</em> create a notification for a <em>specific</em> property being changed, you can add the following code:</p> <pre><code>protected void OnImageFullPathChanged(EventArgs e) { EventHandler handler = ImageFullPathChanged; if (handler != null) handler(this, e); } public event EventHandler ImageFullPathChanged; </code></pre> <p>Then add the line <code>OnImageFullPathChanged(EventArgs.Empty)</code> after the line <code>OnPropertyChanged("ImageFullPath")</code>.</p> <p>Since we have .Net 4.5 there exists the <a href="https://msdn.microsoft.com/en-us/library/system.runtime.compilerservices.callermembernameattribute.aspx" rel="noreferrer"><code>CallerMemberAttribute</code></a>, which allows to get rid of the hard-coded string for the property name in the source code:</p> <pre><code> protected void OnPropertyChanged( [System.Runtime.CompilerServices.CallerMemberName] string propertyName = "") { OnPropertyChanged(new PropertyChangedEventArgs(propertyName)); } public string ImageFullPath { get { return imageFullPath; } set { if (value != imageFullPath) { imageFullPath = value; OnPropertyChanged(); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload