public static DocumentNodePath ResolveNodePathForTemplateWithinExistingStyle(SceneElement targetElement, PropertyReference targetPropertyReference)
        {
            DocumentNodePath documentNodePath = ControlStylingOperations.ProvideStyleOrTemplateNodePath(targetElement, targetPropertyReference);

            if (documentNodePath != null)
            {
                SceneView defaultView = targetElement.ViewModel.DefaultView;
                ICollection <IViewObject> instantiatedElements = defaultView.GetInstantiatedElements(documentNodePath);
                if (instantiatedElements.Count <= 0)
                {
                    return(ControlStylingOperations.FindTemplateWithinStyle(documentNodePath, targetElement, ControlElement.TemplateProperty, (IPropertyId)targetPropertyReference[0]));
                }
                foreach (IViewObject viewObject in (IEnumerable <IViewObject>)instantiatedElements)
                {
                    if (PlatformTypes.Control.IsAssignableFrom((ITypeId)viewObject.GetIType((ITypeResolver)targetElement.ProjectContext)))
                    {
                        object platformObject = viewObject.GetValue(targetElement.ProjectContext.ResolveProperty(ControlElement.TemplateProperty));
                        if (platformObject != null)
                        {
                            IViewObject      instance = targetElement.ViewModel.ProjectContext.Platform.ViewObjectFactory.Instantiate(platformObject);
                            DocumentNodePath correspondingNodePath = defaultView.GetCorrespondingNodePath(instance, true);
                            if (correspondingNodePath != null && ControlStylingOperations.ShouldSetEditingContextToNodePath(correspondingNodePath, targetElement, targetPropertyReference[0]))
                            {
                                return(correspondingNodePath);
                            }
                        }
                    }
                }
            }
            return((DocumentNodePath)null);
        }
        public static DocumentNodePath ProvideStyleOrTemplateNodePath(SceneElement targetElement, PropertyReference targetPropertyReference)
        {
            DocumentNodePath documentNodePath = (DocumentNodePath)null;
            ReferenceStep    targetProperty   = targetPropertyReference[0];
            StyleNode        styleNode        = targetElement as StyleNode;

            if (targetPropertyReference.TargetType.IsAssignableFrom(targetElement.TargetType) || styleNode != null && targetPropertyReference.TargetType.IsAssignableFrom(styleNode.StyleTargetType))
            {
                IViewObjectFactory viewObjectFactory = targetElement.Platform.ViewObjectFactory;
                object             computedValue     = targetElement.GetComputedValue(targetPropertyReference);
                DocumentNodePath   valuePath         = (DocumentNodePath)null;
                if (computedValue != null)
                {
                    IViewObject instance = viewObjectFactory.Instantiate(computedValue);
                    valuePath = targetElement.ViewModel.DefaultView.GetCorrespondingNodePath(instance, true);
                }
                if (valuePath == null && !BaseFrameworkElement.StyleProperty.Equals((object)targetProperty))
                {
                    BaseFrameworkElement frameworkElement = targetElement as BaseFrameworkElement;
                    if (frameworkElement != null)
                    {
                        frameworkElement.FindMissingImplicitStyle();
                    }
                    if (computedValue != null)
                    {
                        IViewObject      instance = viewObjectFactory.Instantiate(computedValue);
                        DocumentNodePath correspondingNodePath = targetElement.ViewModel.DefaultView.GetCorrespondingNodePath(instance, true);
                        if (correspondingNodePath != null)
                        {
                            valuePath = ControlStylingOperations.FindTemplateWithinStyle(correspondingNodePath, targetElement, (IPropertyId)targetProperty, (IPropertyId)targetProperty);
                        }
                    }
                }
                if (ControlStylingOperations.ShouldSetEditingContextToNodePath(valuePath, targetElement, targetProperty))
                {
                    documentNodePath = valuePath;
                }
            }
            if (documentNodePath == null && styleNode != null && targetElement.IsSet(targetPropertyReference) == PropertyState.Set)
            {
                DocumentNodePath valueAsDocumentNode = targetElement.GetLocalValueAsDocumentNode(targetPropertyReference);
                if (ControlStylingOperations.ShouldSetEditingContextToNodePath(valueAsDocumentNode, targetElement, targetProperty))
                {
                    documentNodePath = valueAsDocumentNode;
                }
            }
            return(documentNodePath);
        }