private static bool HasAttribute(XElement cp, string attributeName, ReflectionOnSeparateAppDomainHandler reflectionOnSeparateAppDomain) { bool found = cp.Attribute(attributeName) != null; if (!found) { foreach (var child in cp.Elements()) { string namespaceName = child.Name.NamespaceName; string[] typeAndProperty = child.Name.LocalName.Split('.'); if (typeAndProperty.Length == 2) { // First check if this is the right property. if (typeAndProperty[1].Trim() == attributeName) { // Then make sure this is not an attached property. bool isProperty = reflectionOnSeparateAppDomain.IsAssignableFrom( GeneratingCSharpCode.DefaultXamlNamespace.NamespaceName, "ContentPresenter", namespaceName, typeAndProperty[0]); if (isProperty) { found = true; break; } } } } } return(found); }
static void TraverseNextElement(XElement currentElement, bool isInsideControlTemplate, ReflectionOnSeparateAppDomainHandler reflectionOnSeparateAppDomain) { if (currentElement.Name == GeneratingCSharpCode.DefaultXamlNamespace + "ControlTemplate") { isInsideControlTemplate = true; } if (isInsideControlTemplate && !currentElement.Name.LocalName.Contains(".")) { bool isContentPresenter = reflectionOnSeparateAppDomain.IsAssignableFrom( GeneratingCSharpCode.DefaultXamlNamespace.NamespaceName, "ContentPresenter", currentElement.Name.NamespaceName, currentElement.Name.LocalName); if (isContentPresenter) { if (!HasAttribute(currentElement, "Content", reflectionOnSeparateAppDomain)) { currentElement.Add(new XAttribute("Content", "{TemplateBinding Content}")); } if (!HasAttribute(currentElement, "ContentTemplate", reflectionOnSeparateAppDomain)) { currentElement.Add(new XAttribute("ContentTemplate", "{TemplateBinding ContentTemplate}")); } } } // Recursion: foreach (var childElements in currentElement.Elements()) { TraverseNextElement(childElements, isInsideControlTemplate, reflectionOnSeparateAppDomain); } }