Quantcast
Channel: konaguidance Discussions Rss Feed
Viewing all articles
Browse latest Browse all 34

New Post: Distinguishing PropertyChange event source properties

$
0
0
Wouldn't it be helpful if BindableBase had a method to check if a PropertyChange event is coming from a specific property like:
private void MyViewModelPropertyChanged(object sender, PropertyChangedEventArgs e)
{
    if (this.MyViewModel.IsPropertyChangedForProperty(e, () => this.MyViewModel.MyProperty))
    {
        ...
    }
}

/// <summary>
/// Checks whether the PropertyChanged event for the given args was raised for the given property.
/// </summary>
/// <param name="e">
/// The PropertyChanged event arguments.
/// </param>
/// <param name="propertyLambdaExpression">a lambda expression from a property created
/// using the form "() => property" e.g. GetPropertyBinding(() => CanDoSomething);</param>
/// <typeparam name="T">The type of the property</typeparam>
/// <returns>
/// True if the PropertyChange event was raised for the property specified with the given lambda expression.
/// </returns>
public bool IsPropertyChangedForProperty<T>(PropertyChangedEventArgs e, Expression<Func<T>> propertyLambdaExpression)
{
    return e.PropertyName == GetPropertyName(propertyLambdaExpression);
}
This would use the old Prism-style property name extraction that is refactoring-safe.

Would that be a good pattern, or would it be better to create a custom event for each property that you would want to handle change event from in the code?

Viewing all articles
Browse latest Browse all 34

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>