private static void OnNameScopeSourceProperty(DependencyObject d, DependencyPropertyChangedEventArgs args)
    {
        INameScope nameScope;
        ElementSpy elementSpy = args.NewValue as ElementSpy;

        if (elementSpy != null && elementSpy.Element != null)
        {
            nameScope = NameScope.GetNameScope(elementSpy.Element);
            if (nameScope != null)
            {
                d.Dispatcher.BeginInvoke(new Action <DependencyObject, INameScope>(SetScope),
                                         System.Windows.Threading.DispatcherPriority.Normal,
                                         d, nameScope);
            }
        }
    }
 public static void SetNameScopeSource(DependencyObject obj, ElementSpy value)
 {
     obj.SetValue(NameScopeSourceProperty, value);
 }