jeudi 11 février 2016

Load Usercontrol with Parameter in WPF with reflection

I need to open user control and assign it to a contentcontrol in WPF dynamically. I have written code which opens the page but main problem arises when there are parameters in constructor of user control

e.g. My UserControl1() is without parameters is running properly but what should i do to run UserControl2("A"), UserControl2("B")

from below mentioned code i need to pass paramValue to usercontrol's constructor if it is not empty.

Here is the code:

void button_Click(object sender, RoutedEventArgs e, string objName, string paramValue)
{            
    Assembly asm = Assembly.GetExecutingAssembly();
    string typeName = asm.GetName().Name + "." + objName;
    UserControl ucl = asm.CreateInstance(typeName) as UserControl;     
    contentControl.Content = ucl; 
}





Aucun commentaire:

Enregistrer un commentaire