Skip to content

Instantly share code, notes, and snippets.

@azyobuzin
Created January 29, 2018 14:16
Show Gist options
  • Save azyobuzin/3784560cc91feb161a28b1a6cd6f0c05 to your computer and use it in GitHub Desktop.
Save azyobuzin/3784560cc91feb161a28b1a6cd6f0c05 to your computer and use it in GitHub Desktop.
/// <summary>
/// <see cref="DependencyOnModelPropertyAttribute"/> が指定されたプロパティの変更通知を有効化します。
/// </summary>
protected void EnableAutoPropertyChangedEvent(INotifyPropertyChanged model)
{
PropertyChangedEventListener listener = null;
foreach (var prop in this.GetType().GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance))
{
PropertyChangedEventHandler handler = null;
foreach (var attr in prop.GetCustomAttributes<DependencyOnModelPropertyAttribute>())
{
if (listener == null) listener = new PropertyChangedEventListener(model);
if (handler == null) handler = CreateHandler(prop.Name);
listener.Add(attr.DependentPropertyName, handler);
}
}
if (listener != null) this.CompositeDisposable.Add(listener);
PropertyChangedEventHandler CreateHandler(string propertyName) => (_, __) => this.RaisePropertyChanged(propertyName);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment