复杂的附加属性行为
我有一个对象(装饰器),它为其任何子对象定义附加属性。
到目前为止,我在远程对象上设置/获取附加属性没有问题:
public static readonly DependencyProperty RequiresRoleProperty =
DependencyProperty.RegisterAttached("RequiresRole", typeof (string), typeof (UIElement),
new FrameworkPropertyMetadata(
null,
FrameworkPropertyMetadataOptions.AffectsRender,
OnSetRequiresRole));
[AttachedPropertyBrowsableForChildrenAttribute(IncludeDescendants=true)]
public static string GetRequiresRole(UIElement element)
{
return element.GetValue(RequiresRoleProperty) as string;
}
public static void SetRequiresRole(UIElement element, string val)
{
element.SetValue(RequiresRoleProperty, val);
}
但是,我已经为此附加属性设置了 OnSetCallback 以便我的设置逻辑,但是我需要引用装饰器(MyClass)这个元素是 的孩子。
在回调的类型签名中:
void Callback(DependencyObject d, DependencyPropertyChagnedEventArgs args)
d
是为其设置附加属性的对象。args.NewValue
&args.OldValue
是属性的实际值。
收集对附加属性所属的包含元素的引用的最佳方法是什么?
I've got an object (Decorator) that defines an attached property for any of it's children.
So far I have no issue setting/getting the attached property on the remote object:
public static readonly DependencyProperty RequiresRoleProperty =
DependencyProperty.RegisterAttached("RequiresRole", typeof (string), typeof (UIElement),
new FrameworkPropertyMetadata(
null,
FrameworkPropertyMetadataOptions.AffectsRender,
OnSetRequiresRole));
[AttachedPropertyBrowsableForChildrenAttribute(IncludeDescendants=true)]
public static string GetRequiresRole(UIElement element)
{
return element.GetValue(RequiresRoleProperty) as string;
}
public static void SetRequiresRole(UIElement element, string val)
{
element.SetValue(RequiresRoleProperty, val);
}
However, I've got an OnSetCallback set up for this attached property to so my setup logic, however I need a reference to the decorator(MyClass) this element is a child of.
In the type signiature of the callback:
void Callback(DependencyObject d, DependencyPropertyChagnedEventArgs args)
d
Is the object for which the attached property is set.args.NewValue
&args.OldValue
is the actual value of the property.
What's the best way of collecting a reference to the containing element that the attached property belongs to?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以沿着视觉树向上查找装饰器类型,从 d 开始。这是一个简单的方法,您可以使用:
You can walk up the Visual Tree looking for your Decorator type, starting at d. This is a simple method you can use: