Best way to notify property change when field is depending on another
As far as I know, there is not built in method for that. I usually do like this:
public class Foo : INotifyPropertyChanged
{
private Bar _bar1;
public Bar Item
{
get { return _bar1; }
set
{
SetField(ref _bar1, value);
ItemChanged();
}
}
public string MyString
{
get { return _bar1.Item; }
}
private void ItemChanged()
{
OnPropertyChanged("MyString");
}
}
public class Bar
{
public string Item { get; set; }
}
You don't have the notifying logic inside of the property this way. It is more maintainable in my opinion this way and it is clear what the method does.
Also, I prefer to use this method I found somewhere on SO instead of the hardcoded name in the class (if the name of the property changes, it breaks).
OnPropertyChanged("MyString");
becomes OnPropertyChanged(GetPropertyName(() => MyString));
where GetPropertyName
is:
public static string GetPropertyName<T>(Expression<Func<T>> propertyLambda)
{
if (propertyLambda == null) throw new ArgumentNullException("propertyLambda");
var me = propertyLambda.Body as MemberExpression;
if (me == null)
{
throw new ArgumentException("You must pass a lambda of the form: '() => Class.Property' or '() => object.Property'");
}
return me.Member.Name;
}
After this, every time I change the property as the name, I will have to rename the property everywhere I have GetPropertyName
instead of searching for the hardcoded string values.
I'm also curious about a built-in way to do the dependency, so I'm putting a favorite in there :)
One way is to just call OnPropertyChanged
multiple times:
public MyClass Item
{
get
{
return _item;
}
protected set
{
_item = value;
OnPropertyChanged("Item");
OnPropertyChanged("Field");
}
}
This isn't very maintainable, however. Another option is to add a setter to your get-only property and set it from the other property:
public MyClass Item
{
get
{
return _item;
}
protected set
{
_item = value;
OnPropertyChanged("Item");
Field = _item.Field;
}
}
public object Field
{
get
{
return _field;
}
private set
{
_field = value;
OnPropertyChanged("Field");
}
}
There is no built-in mechanism for using attributes to indicate this relationship between properties, however it would be possible to create a helper class that could do it for you.
I've made a really basic example of what that might look like here:
[AttributeUsage( AttributeTargets.Property )]
public class DepondsOnAttribute : Attribute
{
public DepondsOnAttribute( string name )
{
Name = name;
}
public string Name { get; }
}
public class PropertyChangedNotifier<T> : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public PropertyChangedNotifier( T owner )
{
mOwner = owner;
}
public void OnPropertyChanged( string propertyName )
{
var handler = PropertyChanged;
if( handler != null ) handler( mOwner, new PropertyChangedEventArgs( propertyName ) );
List<string> dependents;
if( smPropertyDependencies.TryGetValue( propertyName, out dependents ) )
{
foreach( var dependent in dependents ) OnPropertyChanged( dependent );
}
}
static PropertyChangedNotifier()
{
foreach( var property in typeof( T ).GetProperties() )
{
var dependsOn = property.GetCustomAttributes( true )
.OfType<DepondsOnAttribute>()
.Select( attribute => attribute.Name );
foreach( var dependency in dependsOn )
{
List<string> list;
if( !smPropertyDependencies.TryGetValue( dependency, out list ) )
{
list = new List<string>();
smPropertyDependencies.Add( dependency, list );
}
if (property.Name == dependency)
throw new ApplicationException(String.Format("Property {0} of {1} cannot depends of itself", dependency, typeof(T).ToString()));
list.Add( property.Name );
}
}
}
private static readonly Dictionary<string, List<string>> smPropertyDependencies = new Dictionary<string, List<string>>();
private readonly T mOwner;
}
This isn't terribly robust (for example you could create a circular dependency between properties and the property changed would get stuck in an infinite recursion situation). It can also be made simpler using some .NET 4.5 and C#6 features, but I'll leave all that as an exercise for the reader. It probably also doesn't handle inheritance very well.
To use this class:
public class Example : INotifyPropertyChanged
{
private MyClass _item;
private PropertyChangedNotifier<Example> _notifier;
public Example()
{
_notifier = new PropertyChangedNotifier<Example>( this );
}
public event PropertyChangedEventHandler PropertyChanged
{
add { _notifier.PropertyChanged += value; }
remove { _notifier.PropertyChanged -= value; }
}
public MyClass Item
{
get
{
return _item;
}
protected set
{
_item = value;
OnPropertyChanged("Item");
}
}
[DependsOn( "Item" )]
public object Field
{
get
{
return _item.Field;
}
}
protected void OnPropertyChanged(string propertyName)
{
_notifier.OnPropertyChanged( propertyName );
}
}