In my WPF application, I have a table which stores the frequently used window names for each user. At runtiime, I make a list of it.
List<string> LstUserWindows= new List<string>();
What I need is I need to open each window depending on the names of the windows in the list. (I am using usercontrols as windows). Something like below:
foreach (var rec in LstUserWindows)
{
UserControl mainUC = this.FindName(rec.MyWindow) as UserControl;
displayUserControls(mainUC,null);
}
I’m not sure which approach you have currently taken in storing the
UserControlinstances, but here are two possible approaches you could take.If all
UserControlinstances already exist within your UI but are simply hidden, then you should be able to use theFindName(...)(as you’ve mentioned in your question) and then change theVisibilityproperty of theUserControl.If you have not yet loaded the
UserControlinstances and you want to dynamically create the control given it’s name, then you need to look into using Reflection. Using this approach, you can acquire theTypeinformation from theAssemblyand work on using Reflection to construct the object. Alternately, you could use theActivatorclass to construct an instance of the required control type. For that approach, you would do something like this.Note: I’m not sure if the parameter structure is correct (I cannot currently test it). Check out the MSDN Documentation for more help on it.