//------------------------------------------------------
        //
        //  Public Methods
        //
        //------------------------------------------------------

        /// <summary> Returns the referenced object. </summary>
        /// <param name="d">Element defining context for the reference. </param>
        /// <param name="args">See ObjectRefArgs </param>
        internal override object GetObject(DependencyObject d, ObjectRefArgs args)
        {
            if (d == null)
            {
                throw new ArgumentNullException("d");
            }

            object o = null;

            if (args.ResolveNamesInTemplate)
            {
                // look in container's template (if any) first
                FrameworkElement fe = d as FrameworkElement;
                if (fe != null && fe.TemplateInternal != null)
                {
                    o = Helper.FindNameInTemplate(_name, d);

                    if (args.IsTracing)
                    {
                        TraceData.Trace(TraceEventType.Warning,
                                        TraceData.ElementNameQueryTemplate(
                                            _name,
                                            TraceData.Identify(d)));
                    }
                }

                if (o == null)
                {
                    args.NameResolvedInOuterScope = true;
                }
            }

            FrameworkObject fo = new FrameworkObject(d);

            while (o == null && fo.DO != null)
            {
                DependencyObject scopeOwner;
                o = fo.FindName(_name, out scopeOwner);

                // if the original element is a scope owner, supports IComponentConnector,
                // and has a parent, don't use the result of FindName.  The
                // element is probably an instance of a Xaml-subclassed control;
                // we want to resolve the name starting in the next outer scope.
                // (bug 1669408)
                // Also, if the element's NavigationService property is locally
                // set, the element is the root of a navigation and should use the
                // inner scope (bug 1765041)
                if (d == scopeOwner && d is IComponentConnector &&
                    d.ReadLocalValue(System.Windows.Navigation.NavigationService.NavigationServiceProperty) == DependencyProperty.UnsetValue)
                {
                    DependencyObject parent = LogicalTreeHelper.GetParent(d);
                    if (parent == null)
                    {
                        parent = Helper.FindMentor(d.InheritanceContext);
                    }

                    if (parent != null)
                    {
                        o = null;
                        fo.Reset(parent);
                        continue;
                    }
                }

                if (args.IsTracing)
                {
                    TraceData.Trace(TraceEventType.Warning,
                                    TraceData.ElementNameQuery(
                                        _name,
                                        TraceData.Identify(fo.DO)));
                }

                if (o == null)
                {
                    args.NameResolvedInOuterScope = true;

                    // move to the next outer namescope.
                    // First try TemplatedParent of the scope owner.
                    FrameworkObject  foScopeOwner = new FrameworkObject(scopeOwner);
                    DependencyObject dd           = foScopeOwner.TemplatedParent;

                    // if that doesn't work, we could be at the top of
                    // generated content for an ItemsControl.  If so, use
                    // the (visual) parent - a panel.
                    if (dd == null)
                    {
                        Panel panel = fo.FrameworkParent.DO as Panel;
                        if (panel != null && panel.IsItemsHost)
                        {
                            dd = panel;
                        }
                    }

                    // if the logical parent is a ContentControl whose content
                    // points right back, move to the ContentControl.   This is the
                    // m---- equivalent of having the ContentControl as the TemplatedParent.
                    // (The InheritanceBehavior clause prevents this for cases where the
                    // parent ContentControl imposes a barrier, e.g. Frame)
                    if (dd == null && scopeOwner == null)
                    {
                        ContentControl cc = LogicalTreeHelper.GetParent(fo.DO) as ContentControl;
                        if (cc != null && cc.Content == fo.DO && cc.InheritanceBehavior == InheritanceBehavior.Default)
                        {
                            dd = cc;
                        }
                    }

                    // next, see if we're in a logical tree attached directly
                    // to a ContentPresenter.  This is the m---- equivalent of
                    // having the ContentPresenter as the TemplatedParent.
                    if (dd == null && scopeOwner == null)
                    {
                        // go to the top of the logical subtree
                        DependencyObject parent;
                        for (dd = fo.DO;;)
                        {
                            parent = LogicalTreeHelper.GetParent(dd);
                            if (parent == null)
                            {
                                parent = Helper.FindMentor(dd.InheritanceContext);
                            }

                            if (parent == null)
                            {
                                break;
                            }

                            dd = parent;
                        }

                        // if it's attached to a ContentPresenter, move to the CP
                        ContentPresenter cp = VisualTreeHelper.IsVisualType(dd) ? VisualTreeHelper.GetParent(dd) as ContentPresenter : null;
                        dd = (cp != null && cp.TemplateInternal.CanBuildVisualTree) ? cp : null;
                    }

                    fo.Reset(dd);
                }
            }

            if (o == null)
            {
                o = DependencyProperty.UnsetValue;
                args.NameResolvedInOuterScope = false;
            }

            return(o);
        }
        // Token: 0x060075FB RID: 30203 RVA: 0x0021A3F0 File Offset: 0x002185F0
        internal override object GetObject(DependencyObject d, ObjectRefArgs args)
        {
            if (d == null)
            {
                throw new ArgumentNullException("d");
            }
            object obj = null;

            if (args.ResolveNamesInTemplate)
            {
                FrameworkElement frameworkElement = d as FrameworkElement;
                if (frameworkElement != null && frameworkElement.TemplateInternal != null)
                {
                    obj = Helper.FindNameInTemplate(this._name, d);
                    if (args.IsTracing)
                    {
                        TraceData.Trace(TraceEventType.Warning, TraceData.ElementNameQueryTemplate(new object[]
                        {
                            this._name,
                            TraceData.Identify(d)
                        }));
                    }
                }
                if (obj == null)
                {
                    args.NameResolvedInOuterScope = true;
                }
            }
            FrameworkObject frameworkObject = new FrameworkObject(d);

            while (obj == null && frameworkObject.DO != null)
            {
                DependencyObject dependencyObject;
                obj = frameworkObject.FindName(this._name, out dependencyObject);
                if (d == dependencyObject && d is IComponentConnector && d.ReadLocalValue(NavigationService.NavigationServiceProperty) == DependencyProperty.UnsetValue)
                {
                    DependencyObject dependencyObject2 = LogicalTreeHelper.GetParent(d);
                    if (dependencyObject2 == null)
                    {
                        dependencyObject2 = Helper.FindMentor(d.InheritanceContext);
                    }
                    if (dependencyObject2 != null)
                    {
                        obj = null;
                        frameworkObject.Reset(dependencyObject2);
                        continue;
                    }
                }
                if (args.IsTracing)
                {
                    TraceData.Trace(TraceEventType.Warning, TraceData.ElementNameQuery(new object[]
                    {
                        this._name,
                        TraceData.Identify(frameworkObject.DO)
                    }));
                }
                if (obj == null)
                {
                    args.NameResolvedInOuterScope = true;
                    FrameworkObject  frameworkObject2  = new FrameworkObject(dependencyObject);
                    DependencyObject dependencyObject3 = frameworkObject2.TemplatedParent;
                    if (dependencyObject3 == null)
                    {
                        Panel panel = frameworkObject.FrameworkParent.DO as Panel;
                        if (panel != null && panel.IsItemsHost)
                        {
                            dependencyObject3 = panel;
                        }
                    }
                    if (dependencyObject3 == null && dependencyObject == null)
                    {
                        ContentControl contentControl = LogicalTreeHelper.GetParent(frameworkObject.DO) as ContentControl;
                        if (contentControl != null && contentControl.Content == frameworkObject.DO && contentControl.InheritanceBehavior == InheritanceBehavior.Default)
                        {
                            dependencyObject3 = contentControl;
                        }
                    }
                    if (dependencyObject3 == null && dependencyObject == null)
                    {
                        dependencyObject3 = frameworkObject.DO;
                        for (;;)
                        {
                            DependencyObject dependencyObject4 = LogicalTreeHelper.GetParent(dependencyObject3);
                            if (dependencyObject4 == null)
                            {
                                dependencyObject4 = Helper.FindMentor(dependencyObject3.InheritanceContext);
                            }
                            if (dependencyObject4 == null)
                            {
                                break;
                            }
                            dependencyObject3 = dependencyObject4;
                        }
                        ContentPresenter contentPresenter = VisualTreeHelper.IsVisualType(dependencyObject3) ? (VisualTreeHelper.GetParent(dependencyObject3) as ContentPresenter) : null;
                        dependencyObject3 = ((contentPresenter != null && contentPresenter.TemplateInternal.CanBuildVisualTree) ? contentPresenter : null);
                    }
                    frameworkObject.Reset(dependencyObject3);
                }
            }
            if (obj == null)
            {
                obj = DependencyProperty.UnsetValue;
                args.NameResolvedInOuterScope = false;
            }
            return(obj);
        }