Justin Seymour Justin Seymour - 1 year ago 41
C# Question

INotifyPropertyChanged with Properties that have the same name

I'm working on a system to represent data. In it we use a templetized interface that implements

INotifyPropertyChanged
.

public interface IScalar<T> : ISignal, INotifyPropertyChanged
{
void Check(T value);

/// <summary>
/// Formats the specified value based on the item's formatting
/// characteristics. Will throw an exception if the specified value
/// could not be properly converted to the underlying type.
/// </summary>
/// <param name="value">Value to format.</param>
/// <returns>Formatted value.</returns>
string Format(T value);

T Value { get; set; }
string Units { get; set; }
}


We end having a class that implements
IScalar<double>
and
IScalar<string>
. Is there a way to make sure the correct PropertyChanged event is fired? It uses a string representation of the property name. And since I have two properties with the same name I can't guarantee the right event will be fired. We are looking to have a grid in WPF bind to a list of IScalar

Answer Source

Apart from Scott's proper answer, you may also want to get away from calling the PropertyChanged Method by passing in strings. Here's how you do that:

protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
    var handler = PropertyChanged;
    if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}

INPC using member/method name is now supported in the attribute class CallerMemberName found in the System.runtime.compilerservices assembly.

You can read more about it here.

ADVANTAGES: It allows you to easily establish a base class once and for all that handles all notifications based only on the method name. The setter methods merely have this line of code:

OnPropertyChanged();    
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download