wpf - WPF 无法从代码访问 DependencyProperty
问题描述
我正在使用 WPF 4.5.2、.Net 4.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
}
此代码最初由 Luke Malpass (AngelSix) 编写
我用过的财产看起来像这样
public class APType : BaseAP<APType , Type> { }
在 Xaml 中:
<UserControl local:APType.Value={x:Type local:SomeType} />
SomeType 是一个普通的类,没什么特别的
在后面的代码中我正在尝试这个:
Type targetType = GetValue( APType.ValueProperty ) as Type;
不幸的是,targetType始终为null。
我究竟做错了什么?
预先感谢
解决方案
您应该GetValue
在附加属性设置后UserControl
调用:Value
Type type = uc.GetValue(APType.ValueProperty) as Type;
XAML:
<UserControl x:Name="uc" local:APType.Value="{x:Type local:SomeType}">
在创建之前无法设置该属性UserControl
。
推荐阅读
- php - Laravel Passport API 和订阅,哪些技术?
- javascript - 是否有一种移动友好的方式来检测 textarea 中的输入按键?[已回答并关闭]
- javascript - Google Linechart 未在 Google Map InfoWindow 内显示 y 轴标签或图例标签
- jquery - 使用 jQuery 和 LeaderLine 连接 div 元素
- excel - 在线教程之后的 VBA 中出现“如果没有结束则阻止”错误
- python - Django:将数据添加到 JWT 有效负载
- python - Python Keras模型输入与层不兼容
- c# - postgres npgsql 如何使整数数组正常工作
- apollo - Apollo Federation 上的两个微服务之间的突变
- python - 让用户猜测存储的单词(字符串)直到他们猜对的代码