Answered load user control from string

  • Wednesday, May 16, 2012 7:02 PM
     
     

    I have UserControls named a1 and  a2, I want to load one of them depending of the value in a string variable

    say if variable named content has value a1 i need to load control a1 and so on. Is there a way to do so?

    I tried using Activator.CreateInstance but it didnt work for me.

    here is the code

    public Popup(string Type)
    {

    InitializeComponent();

    Type type = Type.GetType("Application1.PopupContent." + Type, true, true);
    object obj = Activator.CreateInstance(type, null, null);

    }

    Is there a problem with this code?

    Thanks.

All Replies

  • Wednesday, May 16, 2012 7:49 PM
     
     

    You really have too many types.  You have Type as a type, Type as a string.  I cannot imagine that your code will compile.

    Also in my test (that works) I used the version of the CreateInstance that has the type and a boolean (I used True) and that worked fine.

    Hope this helps

    Lloyd Sheen


    Lloyd Sheen

  • Wednesday, May 16, 2012 8:13 PM
     
      Has Code

    I have UserControls named a1 and  a2, I want to load one of them depending of the value in a string variable

    I tried using Activator.CreateInstance but it didnt work for me.

    Type type = Type.GetType("Application1.PopupContent." + Type, true, true);
    object obj = Activator.CreateInstance(type, null, null);

    Is there a problem with this code?

    Does Type.GetType return a Type instance or does it throw?
    And:Activator.CreateInstance has many overloads.
    Your call lets the compiler chose this one:

    public static Object CreateInstance(
    	Type type,
    	Object[] args,
    	Object[] activationAttributes
    )

    But if you don't specify any arguments and no activation attributes
    calling the simple version is likely better,
    because it's more obvious that you want to call the default constructor:

    //declaration
    public
    static Object CreateInstance( Type type )
    //call
    object obj = Activator.CreateInstance(type);

    Do both of your classes "a1" and "a2" have default constructors?

    Chris

  • Wednesday, May 16, 2012 8:19 PM
     
     

    thanks for you reply, how do you use obj, i want to add the dynamically created user control on a grid in the window, when i try to access obj in immediate window i get message back that obj does not exist in the current context. Any ideas.

    Thanks.

  • Wednesday, May 16, 2012 8:59 PM
     
     

    Can you show us the code that you used (that compiles)?  

    Do you get a type from the GetType?

    Does obj get an object from CreateInstance?

    LS


    Lloyd Sheen

  • Friday, May 18, 2012 8:22 AM
     
      Has Code

    hi,

    here is the code sample to load the usercontrol by its name

     UserControl usrctl = (UserControl)Assembly.GetExecutingAssembly().CreateInstance(String.Format("Application1.PopupContent.{0}", "a1"));

    hope this works fine

    regards

    Jagan


  • Friday, May 18, 2012 4:11 PM
     
     

    Thanks for replying, jagan i'll give ur code a try, sqlguy, i do get a type from gettype but dont get object from createinstance, i will show the code soon after giving it a try again, chris, yes both a1 and a2 have default constructors.

    Thanks

  • Saturday, May 19, 2012 12:23 PM
     
     Answered

    Hi,

    I'd think the way to do this in WPF would be to have two control templates or data templates which contain the different controls, and a TemplateSelector which choses one of them depending on the value of your string property. This way, WPF will take care of instantiating the controls and setting them up in correct relationships to the rest of the visual / logical tree (that's not in your code, yet, is it?), and your strings don't have to be type names...


    http://wpfglue.wordpress.com

  • Monday, June 04, 2012 6:40 AM
    Moderator
     
     
    We are temporarily marking this as "Answer", if you have any concerns or new findings; please feel free to let me know.
    Best regards.

    Annabella Luo[MSFT]
    MSDN Community Support | Feedback to us

  • Monday, June 04, 2012 2:42 PM
     
     

    I have not tried out the recommended solution yet but will post again if i have any issue.

    Thank you.