Find WPF property (DependencyProperty) by name

Use the name to find DependencyProperty.

If there is such a demand, it is required to look through DependencyPropertyDescriptor.

Typically use additional attributes or attribute-dependent manner.

The following examples give additional properties:

 

 The additional properties are registered, use string to enter the property you are looking for

        

public static readonly DependencyProperty AniInvokeParameterProperty = DependencyProperty.RegisterAttached("AniInvokeParameter", typeof(string), typeof(AniInvoke)); public static void SetAniInvokeParameter(DependencyObject d, string value) => d.SetValue(AniInvokeParameterProperty, value); public static string GetAniInvokeParameter(DependencyObject d) => (string)d.GetValue(AniInvokeParameterProperty);

 

Use specific DependencyPropertyDescriptor

 public static readonly DependencyProperty AniInvokePropery = DependencyProperty.RegisterAttached("AniInvoke", typeof(AnimationTimeline), typeof(AniInvoke), new PropertyMetadata(null, AniInvokeCallBack));

        public static void SetAniInvoke(DependencyObject d, AnimationTimeline value) => d.SetValue(AniInvokePropery, value);

        public static AnimationTimeline GetAniInvoke(DependencyObject d) => (AnimationTimeline)d.GetValue(AniInvokePropery);

        private static void AniInvokeCallBack(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var t = d as FrameworkElement;

            var dpName = t.GetValue(AniInvokeParameterProperty) as string;

           
            if (!string.IsNullOrWhiteSpace(dpName))
            {
                var dpd = DependencyPropertyDescriptor.FromName(dpName, t.GetType(), t.GetType());

                var property = dpd.DependencyProperty;

                var ani = (AnimationTimeline)e.NewValue;

                if (property != null && ani != null)

                    t.BeginAnimation (property, ani); 
            } 
           
        }

 

Guess you like

Origin www.cnblogs.com/T-ARF/p/10968441.html