private static void ReplaceTemplateTargetType(DocumentNode root, IList <DocumentCompositeNode> resources, IType targetType)
        {
            DocumentCompositeNode node = root as DocumentCompositeNode;

            if (node == null)
            {
                return;
            }
            DocumentCompositeNode parent = root.Parent;

            if (parent != null && PlatformTypes.Setter.IsAssignableFrom((ITypeId)parent.Type) && (parent.TypeResolver.ResolveProperty(SetterSceneNode.TargetNameProperty) == null || parent.Properties[SetterSceneNode.TargetNameProperty] == null) && (ControlStylingOperations.DoesPropertyAffectRoot((IPropertyId)root.GetValueProperty()) && parent.Properties[SetterSceneNode.ValueProperty] == root))
            {
                if (PlatformTypes.ControlTemplate.IsAssignableFrom((ITypeId)root.Type))
                {
                    node.Properties[ControlTemplateElement.TargetTypeProperty] = (DocumentNode)node.Context.CreateNode(PlatformTypes.Type, (IDocumentNodeValue) new DocumentNodeMemberValue((IMember)targetType));
                }
                else if (DocumentNodeUtilities.IsDynamicResource(root) || DocumentNodeUtilities.IsStaticResource(root))
                {
                    DocumentNode resourceKey = ResourceNodeHelper.GetResourceKey(node);
                    if (resourceKey != null && resources != null)
                    {
                        foreach (DocumentCompositeNode entryNode in (IEnumerable <DocumentCompositeNode>)resources)
                        {
                            if (resourceKey.Equals(ResourceNodeHelper.GetResourceEntryKey(entryNode)))
                            {
                                DocumentCompositeNode documentCompositeNode = entryNode.Properties[DictionaryEntryNode.ValueProperty] as DocumentCompositeNode;
                                if (documentCompositeNode != null)
                                {
                                    if (PlatformTypes.ControlTemplate.IsAssignableFrom((ITypeId)documentCompositeNode.Type))
                                    {
                                        documentCompositeNode.Properties[ControlTemplateElement.TargetTypeProperty] = (DocumentNode)documentCompositeNode.Context.CreateNode(PlatformTypes.Type, (IDocumentNodeValue) new DocumentNodeMemberValue((IMember)targetType));
                                        break;
                                    }
                                    break;
                                }
                                break;
                            }
                        }
                    }
                }
            }
            if (parent != null && DocumentNodeUtilities.IsStyleOrTemplate(root.Type))
            {
                return;
            }
            foreach (DocumentNode root1 in node.ChildNodes)
            {
                ReplaceStyleTemplateCommand.ReplaceTemplateTargetType(root1, resources, targetType);
            }
        }
        private static void StripFormatting(DocumentNode node)
        {
            node.SourceContext = (INodeSourceContext)null;
            DocumentCompositeNode documentCompositeNode = node as DocumentCompositeNode;

            if (documentCompositeNode != null)
            {
                foreach (IPropertyId propertyKey in (IEnumerable <IProperty>)documentCompositeNode.Properties.Keys)
                {
                    documentCompositeNode.ClearContainerContext(propertyKey);
                }
            }
            foreach (DocumentNode node1 in node.ChildNodes)
            {
                ReplaceStyleTemplateCommand.StripFormatting(node1);
            }
        }
        private void CollectExtraReferences(DocumentNode node, ReplaceStyleTemplateCommand.ExtraReferences references)
        {
            if (node.Parent != null && node.IsProperty)
            {
                ReplaceStyleTemplateCommand.EnsureMember((IMember)node.SitePropertyKey, references);
            }
            ReplaceStyleTemplateCommand.EnsureType(node.Type, references);
            IMember valueAsMember = DocumentPrimitiveNode.GetValueAsMember(node);

            if (valueAsMember != null)
            {
                ReplaceStyleTemplateCommand.EnsureMember(valueAsMember, references);
            }
            foreach (DocumentNode node1 in node.ChildNodes)
            {
                this.CollectExtraReferences(node1, references);
            }
        }
        private static void EnsureMember(IMember member, ReplaceStyleTemplateCommand.ExtraReferences references)
        {
            IProperty property = member as IProperty;

            if (property != null)
            {
                ReplaceStyleTemplateCommand.EnsureType(property.PropertyType, references);
            }
            IType type = member as IType;

            if (type != null)
            {
                ReplaceStyleTemplateCommand.EnsureType(type, references);
            }
            else
            {
                ReplaceStyleTemplateCommand.EnsureType(member.DeclaringType, references);
            }
        }
        protected DocumentNode ProvideCurrentStyle(SceneElement targetElement, IType targetType, PropertyReference targetPropertyReference, bool allowDefaultStyle, out IList <DocumentCompositeNode> auxillaryResources)
        {
            auxillaryResources = (IList <DocumentCompositeNode>)null;
            DocumentNode currentStyle = (DocumentNode)null;
            bool         isThemeStyle = false;
            IType        targetType1  = targetType;

            if (!this.TargetProperty.Equals((object)BaseFrameworkElement.StyleProperty))
            {
                IDocumentContext documentContext    = this.SceneViewModel.Document.DocumentContext;
                Type             propertyTargetType = this.SceneViewModel.Document.ProjectContext.MetadataFactory.GetMetadata(this.Type.RuntimeType).GetStylePropertyTargetType((IPropertyId)this.TargetProperty);
                targetType1 = propertyTargetType == (Type)null ? (IType)null : this.SceneViewModel.ProjectContext.GetType(propertyTargetType);
            }
            if (targetElement.IsSet(targetPropertyReference) == PropertyState.Set)
            {
                DocumentNodePath valueAsDocumentNode = targetElement.GetLocalValueAsDocumentNode(targetPropertyReference);
                if (valueAsDocumentNode != null && valueAsDocumentNode.Node is DocumentCompositeNode && PlatformTypes.Style.IsAssignableFrom((ITypeId)valueAsDocumentNode.Node.Type) && (!StyleNode.IsDefaultValue(valueAsDocumentNode.Node) || allowDefaultStyle))
                {
                    StyleNode styleNode = targetElement.ClonePropertyValueAsSceneNode(targetPropertyReference) as StyleNode;
                    if (styleNode != null)
                    {
                        currentStyle       = styleNode.DocumentNode;
                        auxillaryResources = Microsoft.Expression.DesignSurface.Utility.ResourceHelper.FindReferencedResources(valueAsDocumentNode.Node);
                    }
                }
            }
            if (currentStyle == null)
            {
                object obj = this.ResolveCurrentStyle(targetElement, targetPropertyReference, allowDefaultStyle);
                if (obj != null)
                {
                    DocumentNode correspondingDocumentNode = this.SceneView.GetCorrespondingDocumentNode(this.SceneViewModel.ProjectContext.Platform.ViewObjectFactory.Instantiate(obj), true);
                    if (correspondingDocumentNode != null && PlatformTypes.Style.IsAssignableFrom((ITypeId)correspondingDocumentNode.Type))
                    {
                        currentStyle = correspondingDocumentNode;
                    }
                    else if (obj is Style)
                    {
                        StyleNode styleNode = (StyleNode)this.SceneViewModel.CreateSceneNode(obj);
                        if (targetType1 != null)
                        {
                            styleNode.StyleTargetTypeId = targetType1;
                        }
                        currentStyle = styleNode.DocumentNode;
                    }
                }
                if (currentStyle != null && !allowDefaultStyle && StyleNode.IsDefaultValue(currentStyle))
                {
                    currentStyle = (DocumentNode)null;
                }
            }
            if (currentStyle == null)
            {
                object defaultStyleKey = this.GetDefaultStyleKey(targetElement, (ITypeId)targetType1, (IPropertyId)this.TargetProperty);
                if (defaultStyleKey != null)
                {
                    this.ResolveDefaultStyle(targetElement, defaultStyleKey, allowDefaultStyle, out currentStyle, out isThemeStyle, out auxillaryResources);
                }
            }
            if (currentStyle != null && !allowDefaultStyle)
            {
                List <DocumentCompositeNode> list = new List <DocumentCompositeNode>();
                DocumentCompositeNode        node = currentStyle as DocumentCompositeNode;
                IProperty property = this.SceneViewModel.ProjectContext.ResolveProperty(StyleNode.BasedOnProperty);
                while (node != null && property != null && (node.Type.Equals((object)PlatformTypes.Style) && node.Properties.Count == 2) && (node.Properties[(IPropertyId)property] != null && node.Properties[StyleNode.TargetTypeProperty] != null))
                {
                    node = node.Properties[StyleNode.BasedOnProperty] as DocumentCompositeNode;
                    if (node != null)
                    {
                        if (DocumentNodeUtilities.IsDynamicResource((DocumentNode)node) || DocumentNodeUtilities.IsStaticResource((DocumentNode)node))
                        {
                            DocumentNode resourceKey = ResourceNodeHelper.GetResourceKey(node);
                            if (resourceKey != null && auxillaryResources != null)
                            {
                                foreach (DocumentCompositeNode entryNode in (IEnumerable <DocumentCompositeNode>)auxillaryResources)
                                {
                                    if (resourceKey.Equals(ResourceNodeHelper.GetResourceEntryKey(entryNode)))
                                    {
                                        node = entryNode.Properties[DictionaryEntryNode.ValueProperty] as DocumentCompositeNode;
                                        break;
                                    }
                                }
                            }
                        }
                        if (PlatformTypes.Style.IsAssignableFrom((ITypeId)node.Type))
                        {
                            currentStyle = (DocumentNode)node;
                            list.Add(node);
                        }
                    }
                }
                if (auxillaryResources != null)
                {
                    foreach (DocumentNode documentNode in list)
                    {
                        DocumentCompositeNode parent = documentNode.Parent;
                        if (parent != null && parent.Type.Equals((object)PlatformTypes.DictionaryEntry))
                        {
                            auxillaryResources.Remove(parent);
                        }
                    }
                }
                if (currentStyle != null && (list.Count > 0 || currentStyle.DocumentRoot != null))
                {
                    currentStyle = currentStyle.Clone(targetElement.DocumentContext);
                }
                if (auxillaryResources != null)
                {
                    for (int index = 0; index < auxillaryResources.Count; ++index)
                    {
                        if (auxillaryResources[index].DocumentRoot != null)
                        {
                            auxillaryResources[index] = (DocumentCompositeNode)auxillaryResources[index].Clone(targetElement.DocumentContext);
                        }
                    }
                }
                if (isThemeStyle)
                {
                    ReplaceStyleTemplateCommand.StripFormatting(currentStyle);
                    if (auxillaryResources != null)
                    {
                        for (int index = 0; index < auxillaryResources.Count; ++index)
                        {
                            ReplaceStyleTemplateCommand.StripFormatting((DocumentNode)auxillaryResources[index]);
                        }
                    }
                }
                DocumentCompositeNode documentCompositeNode = currentStyle as DocumentCompositeNode;
                if (documentCompositeNode != null && targetType1 != null)
                {
                    documentCompositeNode.Properties[StyleNode.TargetTypeProperty] = (DocumentNode)documentCompositeNode.Context.CreateNode(PlatformTypes.Type, (IDocumentNodeValue) new DocumentNodeMemberValue((IMember)targetType1));
                }
                if (targetType1 != null)
                {
                    ReplaceStyleTemplateCommand.ReplaceTemplateTargetType(currentStyle, auxillaryResources, targetType1);
                }
            }
            return(currentStyle);
        }