复杂的附加属性行为

发布于 2024-09-15 07:27:41 字数 1324 浏览 6 评论 0原文

我有一个对象(装饰器),它为其任何子对象定义附加属性。

到目前为止,我在远程对象上设置/获取附加属性没有问题:

        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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绝不服输 2024-09-22 07:27:41

您可以沿着视觉树向上查找装饰器类型,从 d 开始。这是一个简单的方法,您可以使用:

public static T FindAncestor<T>(DependencyObject dependencyObject)
    where T : class
{
    DependencyObject target = dependencyObject;
    do
    {
        target = VisualTreeHelper.GetParent(target);
    }
    while (target != null && !(target is T));
    return target as T;
}

You can walk up the Visual Tree looking for your Decorator type, starting at d. This is a simple method you can use:

public static T FindAncestor<T>(DependencyObject dependencyObject)
    where T : class
{
    DependencyObject target = dependencyObject;
    do
    {
        target = VisualTreeHelper.GetParent(target);
    }
    while (target != null && !(target is T));
    return target as T;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文