【发布时间】:2010-12-16 22:19:23
【问题描述】:
是否可以从附加属性类型上的属性更改回调中获取附加属性的实例?
换句话说,如果你有:
public class MyAttachedPropertyClass
{
public static readonly DependencyProperty MyProperty = DependencyProperty.RegisterAttached(
"My", typeof(int), typeof(MyAttachedPropertyClass), new FrameworkPropertyMetadata(0, OnMyPropertyChanged));
private static void OnMyPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
//TODO: access instance of MyAttachedPropertyClass
}
}
我需要访问 MyAttachedPropertyClass 的实例,以便我可以设置该类型上的另一个属性的值。
【问题讨论】:
-
不确定我是否了解您想要做什么。你能详细说明一下吗?
-
我在 MyAttachedPropertyClass 上有一个基于实例的字段,我需要将 OnMyPropertyChanged 中该字段的值设置为整数 e.NewValue 的值。因为 OnMyPropertyChanged 是静态的,所以我无法在静态上下文中访问此实例字段。
-
我不确定我是否理解你的问题,或者你想做什么,但如果你只需要发生属性更改的对象实例(我对你的意思的最佳猜测远),这是您的“DependencyObject d”参数。
-
我想我在这里找到了你的答案:stackoverflow.com/questions/2453146/…
标签: c# .net wpf attached-properties