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.NewValueargs.OldValueはプロパティの実際の値です。

アタッチされたプロパティが属する包含要素への参照を収集する最良の方法は何ですか?

4

1 に答える 1

2

ビジュアル ツリーをたどって、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;
}
于 2010-08-17T22:05:19.720 に答える