none
Nested/Inner Classes and the x:Type Markup Extension

    Question

  • I posted this on the Usnet discussion, but there seems to be more action here, so reposting...

    I want to use an TargetType="{x:Type Yadda.Stuff}" where Yadda  is a
    class and Stuff is an inner class of Yadda.  Is this possible in Xaml?
    I'd rather not have the namespace polluted with a wrapper class that
    I'm using to populate a TreeView (as I can't modify the data class
    itself).

    Any help is appreciated.

    Monday, September 11, 2006 9:53 PM

Answers

  • Sorry, you cannot refer to nested types in XAML via x:Type or anyother type reference.

    If you don't want to do the "pollution", you could build a new markupExtension.  Like this (read to end before doing...):

    using System;
    using System.Windows.Markup;

    namespace Microsoft.Samples.RobRelyea.WpfUtilities
    {
        public class Type2Extension : System.Windows.Markup.TypeExtension
        
    {

            public Type2Extension()
            {
            }

            public Type2Extension(string typeName)
            {
               
    base.TypeName = typeName;
           
    }

            public override object ProvideValue(IServiceProvider serviceProvider)
            {
               
    IXamlTypeResolver typeResolver = (IXamlTypeResolver)serviceProvider.GetService(typeof(IXamlTypeResolver));
               
    int period = TypeName.IndexOf('.');
               
    if (period < 0)
                   
    return typeResolver.Resolve(TypeName);
               
    else
               
    {
                   
    Type outerType = typeResolver.Resolve(TypeName.Substring(0,period));
                   
    return outerType.Assembly.GetType(outerType.FullName + "." + TypeName.Substring(period + 1));
               
    }
           
    }
       
    }
    }

     

    TargetType="{my:Type2 Yadda.Stuff}"

    I just built that and tested it in normal XAML.  It worked fine.

    But then I tried it inside of a Style.

    I think that there is a bug (at least with the RC1 build and very likely with the RTM build as well) that prevents it from working within a Style (where you seem to want to use it.)  (Unforunately our Style parsing shares some but not all code with the normal parser.)

    LIkely your best course of action is to "pollute".

    Sorry,

    Rob Relyea
    Program Manager, WPF Team
    http://rrelyea.spaces.live.com

     

    Monday, September 11, 2006 11:20 PM
    Moderator
  • Thanks for the quick response.  I suppose as a workaround, I could create a sub namespace for these classes ala:

    namespace App {
        namespace Window1.Helpers {
            internal class TreeItemWrapper {}
        }

        class Window1 : Window {}

    }

    It's not perfect, but it will probably work for me.

    Tuesday, September 12, 2006 12:07 AM

All replies

  • Sorry, you cannot refer to nested types in XAML via x:Type or anyother type reference.

    If you don't want to do the "pollution", you could build a new markupExtension.  Like this (read to end before doing...):

    using System;
    using System.Windows.Markup;

    namespace Microsoft.Samples.RobRelyea.WpfUtilities
    {
        public class Type2Extension : System.Windows.Markup.TypeExtension
        
    {

            public Type2Extension()
            {
            }

            public Type2Extension(string typeName)
            {
               
    base.TypeName = typeName;
           
    }

            public override object ProvideValue(IServiceProvider serviceProvider)
            {
               
    IXamlTypeResolver typeResolver = (IXamlTypeResolver)serviceProvider.GetService(typeof(IXamlTypeResolver));
               
    int period = TypeName.IndexOf('.');
               
    if (period < 0)
                   
    return typeResolver.Resolve(TypeName);
               
    else
               
    {
                   
    Type outerType = typeResolver.Resolve(TypeName.Substring(0,period));
                   
    return outerType.Assembly.GetType(outerType.FullName + "." + TypeName.Substring(period + 1));
               
    }
           
    }
       
    }
    }

     

    TargetType="{my:Type2 Yadda.Stuff}"

    I just built that and tested it in normal XAML.  It worked fine.

    But then I tried it inside of a Style.

    I think that there is a bug (at least with the RC1 build and very likely with the RTM build as well) that prevents it from working within a Style (where you seem to want to use it.)  (Unforunately our Style parsing shares some but not all code with the normal parser.)

    LIkely your best course of action is to "pollute".

    Sorry,

    Rob Relyea
    Program Manager, WPF Team
    http://rrelyea.spaces.live.com

     

    Monday, September 11, 2006 11:20 PM
    Moderator
  • Thanks for the quick response.  I suppose as a workaround, I could create a sub namespace for these classes ala:

    namespace App {
        namespace Window1.Helpers {
            internal class TreeItemWrapper {}
        }

        class Window1 : Window {}

    }

    It's not perfect, but it will probably work for me.

    Tuesday, September 12, 2006 12:07 AM
  • You can actually just use

    {x:Type Window1+Helpers}

    In other words, use + instead of .
    Friday, December 29, 2006 3:28 PM
  • When I use {x:Static Types1:Enums+WindowModeEnum.Edit} format code compiles and works fine.  But I can't open code in designer.  I am getting following error.

     

    Type 'Types1:Enums+WindowModeEnum' was not found.

       at MS.Internal.Metadata.ExposedTypes.ValueSerializers.StaticMemberDocumentValueSerializer.ConvertToDocumentValue(ITypeMetadata type, String value, IServiceProvider documentServices)

       at MS.Internal.Design.DocumentModel.DocumentTrees.Markup.XamlMarkupExtensionPropertyBase.get_Value()

       at MS.Internal.Design.DocumentModel.DocumentTrees.DocumentPropertyWrapper.get_Value()

       at MS.Internal.Design.DocumentModel.DocumentTrees.InMemory.InMemoryDocumentProperty..ctor(DocumentProperty property, InMemoryDocumentItem item)

       at MS.Internal.Design.DocumentModel.DocumentTrees.InMemory.InMemoryDocumentItem.SetUpItem(DocumentItem item)

     

    Thanks a lot.

     

    Sincerely,

    Vlad.

    Tuesday, June 01, 2010 6:52 PM
  • We had somebody look at the use of {x:Type Foo+Bar} pattern and test it in VS2010 and Blend4. It appears that it works fine at Runtime, CompileTime, in Blend 4, but fails in VS2010's WPF Designer.

    We've filed a bug, and routed it to the WPF Designer team.

    Thanks, Rob
    WPF Team
    http://robrelyea.com/blog | @rrelyea (twitter)

    Thursday, June 03, 2010 5:30 PM
    Moderator
  • Rob,

    Thanks for the update.  Is there anywhere that we can track the progress of this bug or voice our support for having it resolved.  It's a pretty nasty limitation of the WPF Designer in VS2010.

    Sean

    Friday, December 03, 2010 2:18 PM