Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
WPF无法从代码访问DependencyProperty_Wpf_Attached Properties - Fatal编程技术网

WPF无法从代码访问DependencyProperty

WPF无法从代码访问DependencyProperty,wpf,attached-properties,Wpf,Attached Properties,我使用的是WPF4.5.2、.NET4.7.2、C#7 这是我的附加属性基类的代码 public abstract class BaseAP<Parent, Property> where Parent : BaseAP<Parent , Property>, new() { #region Public Events /// <summary> /// Fire when the value

我使用的是WPF4.5.2、.NET4.7.2、C#7

这是我的附加属性基类的代码

   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
    }
不幸的是,targetType总是空的

我做错了什么


感谢使用avance

设置
附加属性后,您应该在
用户控件
上调用
获取值

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

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


在创建
UserControl
之前,无法设置属性。

在哪里调用
GetValue
?在UserControl的代码隐藏中,可以将泛型参数属性设置为任何类型。我正在使用它,例如用于布尔、int甚至自定义类。在这个特殊的例子中,我指的是Type,因为我需要访问local:sometype的一些元数据。在构建
UserControl
之前,无法设置该属性。正如我在回答中所说的,如果你在物业被损坏后再打电话给它,它会正常工作。不要再破坏这个问题。
Type targetType = GetValue( APType.ValueProperty ) as Type;
Type type = uc.GetValue(APType.ValueProperty) as Type;
<UserControl x:Name="uc" local:APType.Value="{x:Type local:SomeType}">