@ -146,6 +146,8 @@ namespace ICSharpCode.WpfDesign.XamlDom
static Type FindType ( XamlTypeFinder typeFinder , string namespaceUri , string localName )
static Type FindType ( XamlTypeFinder typeFinder , string namespaceUri , string localName )
{
{
Type elementType = typeFinder . GetType ( namespaceUri , localName ) ;
Type elementType = typeFinder . GetType ( namespaceUri , localName ) ;
if ( elementType = = null )
elementType = typeFinder . GetType ( namespaceUri , localName + "Extension" ) ;
if ( elementType = = null )
if ( elementType = = null )
throw new XamlLoadException ( "Cannot find type " + localName + " in " + namespaceUri ) ;
throw new XamlLoadException ( "Cannot find type " + localName + " in " + namespaceUri ) ;
return elementType ;
return elementType ;
@ -195,7 +197,7 @@ namespace ICSharpCode.WpfDesign.XamlDom
if ( typeof ( FrameworkTemplate ) . IsAssignableFrom ( elementType ) )
if ( typeof ( FrameworkTemplate ) . IsAssignableFrom ( elementType ) )
{
{
var xamlObj = new XamlObject ( document , element , elementType , TemplateHelper . GetFrameworkTemplate ( element ) ) ;
var xamlObj = new XamlObject ( document , element , elementType , TemplateHelper . GetFrameworkTemplate ( element , currentXamlObject ) ) ;
xamlObj . ParentObject = currentXamlObject ;
xamlObj . ParentObject = currentXamlObject ;
return xamlObj ;
return xamlObj ;
}
}
@ -323,8 +325,11 @@ namespace ICSharpCode.WpfDesign.XamlDom
if ( defaultProperty = = null & & obj . Instance ! = null & & CollectionSupport . IsCollectionType ( obj . Instance . GetType ( ) ) ) {
if ( defaultProperty = = null & & obj . Instance ! = null & & CollectionSupport . IsCollectionType ( obj . Instance . GetType ( ) ) ) {
XamlObject parentObj = obj . ParentObject ;
XamlObject parentObj = obj . ParentObject ;
var parentElement = element . ParentNode ;
var parentElement = element . ParentNode ;
XamlPropertyInfo propertyInfo = GetPropertyInfo ( settings . TypeFinder , parentObj . Instance , parentObj . ElementType , parentElement . NamespaceURI , parentElement . LocalName ) ;
XamlPropertyInfo propertyInfo ;
if ( parentObj ! = null ) {
propertyInfo = GetPropertyInfo ( settings . TypeFinder , parentObj . Instance , parentObj . ElementType , parentElement . NamespaceURI , parentElement . LocalName ) ;
collectionProperty = FindExistingXamlProperty ( parentObj , propertyInfo ) ;
collectionProperty = FindExistingXamlProperty ( parentObj , propertyInfo ) ;
}
collectionInstance = obj . Instance ;
collectionInstance = obj . Instance ;
collectionType = obj . ElementType ;
collectionType = obj . ElementType ;
collectionPropertyElement = element ;
collectionPropertyElement = element ;
@ -500,6 +505,7 @@ namespace ICSharpCode.WpfDesign.XamlDom
if ( eventInfo ! = null ) {
if ( eventInfo ! = null ) {
return new XamlEventPropertyInfo ( eventInfo ) ;
return new XamlEventPropertyInfo ( eventInfo ) ;
}
}
throw new XamlLoadException ( "property " + propertyName + " not found" ) ;
throw new XamlLoadException ( "property " + propertyName + " not found" ) ;
}
}
@ -507,7 +513,7 @@ namespace ICSharpCode.WpfDesign.XamlDom
{
{
MethodInfo getMethod = elementType . GetMethod ( "Get" + propertyName , BindingFlags . Public | BindingFlags . Static ) ;
MethodInfo getMethod = elementType . GetMethod ( "Get" + propertyName , BindingFlags . Public | BindingFlags . Static ) ;
MethodInfo setMethod = elementType . GetMethod ( "Set" + propertyName , BindingFlags . Public | BindingFlags . Static ) ;
MethodInfo setMethod = elementType . GetMethod ( "Set" + propertyName , BindingFlags . Public | BindingFlags . Static ) ;
if ( getMethod ! = null & & setMethod ! = null ) {
if ( getMethod ! = null | | setMethod ! = null ) {
FieldInfo field = elementType . GetField ( propertyName + "Property" , BindingFlags . Public | BindingFlags . Static ) ;
FieldInfo field = elementType . GetField ( propertyName + "Property" , BindingFlags . Public | BindingFlags . Static ) ;
if ( field ! = null & & field . FieldType = = typeof ( DependencyProperty ) ) {
if ( field ! = null & & field . FieldType = = typeof ( DependencyProperty ) ) {
return new XamlDependencyPropertyInfo ( ( DependencyProperty ) field . GetValue ( null ) , true ) ;
return new XamlDependencyPropertyInfo ( ( DependencyProperty ) field . GetValue ( null ) , true ) ;
@ -521,9 +527,28 @@ namespace ICSharpCode.WpfDesign.XamlDom
return null ;
return null ;
}
}
internal static XamlPropertyInfo TryFindAttachedEvent ( Type elementType , string propertyName )
{
FieldInfo fieldEvent = elementType . GetField ( propertyName + "Event" , BindingFlags . Public | BindingFlags . Static ) ;
if ( fieldEvent ! = null & & fieldEvent . FieldType = = typeof ( RoutedEvent ) )
{
return new XamlEventPropertyInfo ( TypeDescriptor . GetEvents ( elementType ) [ propertyName ] ) ;
}
if ( elementType . BaseType ! = null )
{
return TryFindAttachedEvent ( elementType . BaseType , propertyName ) ;
}
return null ;
}
static XamlPropertyInfo FindAttachedProperty ( Type elementType , string propertyName )
static XamlPropertyInfo FindAttachedProperty ( Type elementType , string propertyName )
{
{
XamlPropertyInfo pi = TryFindAttachedProperty ( elementType , propertyName ) ;
XamlPropertyInfo pi = TryFindAttachedProperty ( elementType , propertyName ) ;
if ( pi = = null ) {
pi = TryFindAttachedEvent ( elementType , propertyName ) ;
}
if ( pi ! = null ) {
if ( pi ! = null ) {
return pi ;
return pi ;
} else {
} else {
@ -555,8 +580,13 @@ namespace ICSharpCode.WpfDesign.XamlDom
return FindAttachedProperty ( typeof ( DesignTimeProperties ) , attribute . LocalName ) ;
return FindAttachedProperty ( typeof ( DesignTimeProperties ) , attribute . LocalName ) ;
} else if ( attribute . LocalName = = "IsLocked" & & attribute . NamespaceURI = = XamlConstants . DesignTimeNamespace ) {
} else if ( attribute . LocalName = = "IsLocked" & & attribute . NamespaceURI = = XamlConstants . DesignTimeNamespace ) {
return FindAttachedProperty ( typeof ( DesignTimeProperties ) , attribute . LocalName ) ;
return FindAttachedProperty ( typeof ( DesignTimeProperties ) , attribute . LocalName ) ;
} else if ( attribute . LocalName = = "LayoutOverrides" & & attribute . NamespaceURI = = XamlConstants . DesignTimeNamespace ) {
return FindAttachedProperty ( typeof ( DesignTimeProperties ) , attribute . LocalName ) ;
} else if ( attribute . LocalName = = "LayoutRounding" & & attribute . NamespaceURI = = XamlConstants . DesignTimeNamespace ) {
return FindAttachedProperty ( typeof ( DesignTimeProperties ) , attribute . LocalName ) ;
}
}
return null ;
return null ;
}
}
@ -768,6 +798,19 @@ namespace ICSharpCode.WpfDesign.XamlDom
/// <param name="settings">Parser settings used by <see cref="XamlParser"/>.</param>
/// <param name="settings">Parser settings used by <see cref="XamlParser"/>.</param>
/// <returns>Returns the XamlObject of the parsed <paramref name="xaml"/>.</returns>
/// <returns>Returns the XamlObject of the parsed <paramref name="xaml"/>.</returns>
public static XamlObject ParseSnippet ( XamlObject root , string xaml , XamlParserSettings settings )
public static XamlObject ParseSnippet ( XamlObject root , string xaml , XamlParserSettings settings )
{
return ParseSnippet ( root , xaml , settings , null ) ;
}
/// <summary>
/// Method use to parse a piece of Xaml.
/// </summary>
/// <param name="root">The Root XamlObject of the current document.</param>
/// <param name="xaml">The Xaml being parsed.</param>
/// <param name="settings">Parser settings used by <see cref="XamlParser"/>.</param>
/// <param name="parentObject">Parent Object, where the Parsed snippet will be inserted (Needed for Example for Bindings).</param>
/// <returns>Returns the XamlObject of the parsed <paramref name="xaml"/>.</returns>
public static XamlObject ParseSnippet ( XamlObject root , string xaml , XamlParserSettings settings , XamlObject parentObject )
{
{
XmlTextReader reader = new XmlTextReader ( new StringReader ( xaml ) ) ;
XmlTextReader reader = new XmlTextReader ( new StringReader ( xaml ) ) ;
var element = root . OwnerDocument . XmlDocument . ReadNode ( reader ) ;
var element = root . OwnerDocument . XmlDocument . ReadNode ( reader ) ;
@ -785,6 +828,7 @@ namespace ICSharpCode.WpfDesign.XamlDom
parser . settings = settings ;
parser . settings = settings ;
parser . errorSink = ( IXamlErrorSink ) settings . ServiceProvider . GetService ( typeof ( IXamlErrorSink ) ) ;
parser . errorSink = ( IXamlErrorSink ) settings . ServiceProvider . GetService ( typeof ( IXamlErrorSink ) ) ;
parser . document = root . OwnerDocument ;
parser . document = root . OwnerDocument ;
parser . currentXamlObject = parentObject ;
var xamlObject = parser . ParseObject ( element as XmlElement ) ;
var xamlObject = parser . ParseObject ( element as XmlElement ) ;
RemoveRootNamespacesFromNodeAndChildNodes ( root , element ) ;
RemoveRootNamespacesFromNodeAndChildNodes ( root , element ) ;