Last active
September 15, 2017 10:58
-
-
Save usausa/d7b7b93da99bd112ec302a7f2c0155dc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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