2012-01-17 52 views
1

在我的WPF應用程序中,我有一個存儲每個用戶經常使用的窗口名稱的表。在跑步時,我列出了它。在運行時打開usercontrol-WPF?

List<string> LstUserWindows= new List<string>(); 

我需要的是我需要打開每個窗口取決於列表中的窗口的名稱。 (我正在使用usercontrols作爲窗口)。類似下面:

foreach (var rec in LstUserWindows) 
{ 
     UserControl mainUC = this.FindName(rec.MyWindow) as UserControl; 
     displayUserControls(mainUC,null); 
} 

回答

1

我不知道哪種方法你目前採取的存儲UserControl情況,但在這裏,您可以採取兩種可能的方法。

  1. 如果你的UI中已存在的所有UserControl實例,但只是隱藏起來,那麼你應該能夠使用FindName(...)(如你在你的問題已經提到),然後更改UserControlVisibility財產。

  2. 如果你還沒有加載UserControl實例,並且你想動態地創建控制名稱,那麼你需要使用Reflection。使用此方法,您可以從Assembly獲取Type信息,並使用Reflection構建該對象。或者,您可以使用Activator類構造所需控件類型的實例。對於這種方法,你會做這樣的事情。

    foreach (var rec in LstUserWindows) 
    { 
        UserControl control = (UserControl)System.Activator.CreateInstance("AssemblyName", rec); 
        displayUserControls(control, null); 
    } 
    

注:我不知道,如果參數結構是正確的(我目前還不能對其進行測試)。查看MSDN Documentation獲取更多幫助。