Skip to content

Instantly share code, notes, and snippets.

@usausa
Last active September 15, 2017 10:58
Show Gist options
  • Save usausa/d7b7b93da99bd112ec302a7f2c0155dc to your computer and use it in GitHub Desktop.
Save usausa/d7b7b93da99bd112ec302a7f2c0155dc to your computer and use it in GitHub Desktop.
using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Interactivity;
[TypeConstraint(typeof(FrameworkElement))]
public class UpdateTargetAction : TriggerAction<FrameworkElement>
{
public static readonly DependencyProperty PropertyNameProperty =
DependencyProperty.Register(nameof(PropertyName), typeof(string), typeof(UpdateTargetAction));
private DependencyPropertyDescriptor dpd;
public string PropertyName
{
get => (string)GetValue(PropertyNameProperty);
set => SetValue(PropertyNameProperty, value);
}
protected override void OnAttached()
{
base.OnAttached();
UpdatePropertyDescriptor();
}
protected override void OnDetaching()
{
dpd = null;
base.OnDetaching();
}
protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e)
{
UpdatePropertyDescriptor();
}
private void UpdatePropertyDescriptor()
{
dpd = null;
if ((AssociatedObject != null) && !String.IsNullOrEmpty(PropertyName))
{
dpd = DependencyPropertyDescriptor.FromName(PropertyName, AssociatedObject.GetType(), AssociatedObject.GetType());
}
}
protected override void Invoke(object parameter)
{
if (dpd == null)
{
return;
}
var binding = AssociatedObject.GetBindingExpression(dpd.DependencyProperty);
binding?.UpdateTarget();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment