Amit K

Amit K

  • NA
  • 3
  • 5.5k

Loading user controls dynamically in Mainwindow

Jul 28 2016 4:36 PM
'm working on a C# WPF application.I've a list of user controls UC1,UC2 and UC3.And all internally refer to another common user control called UCB. And my App.xaml refers to MainWindow.xml
UCB has a dropdown list with names of different account types.
I need to programmatically show the user control in MainWindow depending upon the account type selected in the Base user control UCB.How do I achieve this please?
My thoughts so far: May be I can instantiate all my user controls in MainWindow.xaml.cs file like this:
UserControl uc1 = (UserControl)assembly.CreateInstance(string.Format("{0}.MyUC1", type.Namespace));
UserControl uc2 = (UserControl)assembly.CreateInstance(string.Format("{0}.MyUC2", type.Namespace));
userControls.Add("1", uc1);
userControls.Add("2", uc2);
and then use the MainWindow.xaml's content property to set the desired user control based on the dropdown value.
this.Content = userControls["1"];
But how do I access this content property inside ComboBox_SelectionChanged event in UCB control??
Please advise.
Thanks.

Answers (1)