-3

I'm using WPF 4.5.2, .Net 4.7.2, C# 7

This is the code of my base class fro attached properties

   public abstract class BaseAP<Parent, Property> where Parent : BaseAP<Parent , Property>, new()
    {

        #region Public Events


        /// <summary>
        /// Fire when the value changes
        /// </summary>
        public event Action<DependencyObject , DependencyPropertyChangedEventArgs> ValueChanged = ( sender , e ) => { };


        #endregion


        #region Properties

        /// <summary>
        /// A singleton instance of the parent class
        /// </summary>
        public static Parent Instance { get; private set; } = new Parent();


        #endregion


        #region Attached Properties Definitions

        /// <summary>
        /// The Attached Property for this class
        /// </summary>
        public static readonly DependencyProperty ValueProperty = DependencyProperty.RegisterAttached( "Value" , typeof( Property ) , typeof( BaseAP<Parent , Property> ) , new PropertyMetadata( new PropertyChangedCallback( OnValuePropertyChanged ) ) );


        /// <summary>
        /// The callback event when the <see cref="ValueProperty"/> is changed
        /// </summary>
        /// <param name="d">The UI-Element that had it's property changed</param>
        /// <param name="e">The arguments for the event</param>
        private static void OnValuePropertyChanged( DependencyObject d , DependencyPropertyChangedEventArgs e )
        {
            // --- Call the parent function
            Instance.OnValueChanged( d , e );


            // --- Call the event listeners
            Instance.ValueChanged( d , e );            
        }




        /// <summary>
        /// Gets the attached property
        /// </summary>
        /// <param name="d">The element to get the property from</param>
        /// <returns></returns>
        public static Property GetValue( DependencyObject d )
        {
            return ( (Property) d.GetValue( ValueProperty ) );
        }

        /// <summary>
        /// Sets the attached property
        /// </summary>
        /// <param name="d">The element to set the property to</param>
        /// <param name="value">The value to set to the element</param>
        public static void SetValue( DependencyObject d , Property value )
        {
            d.SetValue( ValueProperty , value );
        }

        #endregion


        #region Event Methods


        /// <summary>
        /// The method is called when any attached property of this type is changed
        /// </summary>
        /// <param name="d">The ui element that this property was changed for</param>
        /// <param name="e">The arguments for this event</param>
        public virtual void OnValueChanged( DependencyObject d , DependencyPropertyChangedEventArgs e )
        {
            SetValue( d , (Property) e.NewValue );
        }


        #endregion
    }

This code was written originally by Luke Malpass (AngelSix)

My used property looks like this

 public class APType : BaseAP<APType , Type> { }

In Xaml:

<UserControl local:APType.Value={x:Type local:SomeType} />

SomeType is a regular class, nothing special

In code behind I'm trying this:

Type targetType = GetValue( APType.ValueProperty ) as Type;

Unfortunately, targetType is always null.

What am I doing wrong?

Thanks in avance

11
  • Where are you calling GetValue?
    – mm8
    Sep 20, 2019 at 12:02
  • In the code behind of the UserControl
    – GeorgeDuke
    Sep 20, 2019 at 12:16
  • The generic parameter Property can be set to any type. I'm using it e.g. for boolean, int and even custom classes. In this particular case I'm refering to Type, because I need to access some metadata of local:SomeType
    – GeorgeDuke
    Sep 20, 2019 at 12:26
  • 1
    The property cannot be set set before the UserControl has been constructed. It works as expected if you call it after the property has been as I stated in my answer.
    – mm8
    Sep 20, 2019 at 12:32
  • 1
    Do not vandalize this question again. Nov 17, 2019 at 8:14

1 Answer 1

0

You should call GetValue on the UserControl after the Value attached property has been set:

Type type = uc.GetValue(APType.ValueProperty) as Type;

XAML:

<UserControl x:Name="uc" local:APType.Value="{x:Type local:SomeType}">

The property cannot be set before the UserControl has been created.

Not the answer you're looking for? Browse other questions tagged or ask your own question.