2015-08-18 40 views
0

我創建使用CodePlex DLL MDI形式,我添加了一個WPF用戶控制和下面的代碼打開了它作爲一個孩子:如何在點擊按鈕關閉唯一的孩子在WPF

MainMdiContainer.Children.Add(new MdiChild() 
     { 
      Title = " Employee Master", 
      Height = 300, 
      Width = 500, 
      //Here CustomerMaster is the class that you have created for mainWindow.xaml user control. 
      Content = new EmpMaster() 
     }); 

現在我想僅在點擊一個button.I的關閉子窗體(WPF用戶控件)使用了下列那個按鈕點擊代碼:

Window.GetWindow(this).Close(); 

但是,代碼關閉程序所有打開的窗口和MDI家長也一起。 有人可以幫助我關閉按鈕單擊特定的WPF用戶控件。 在此先感謝

+0

如果您執行'this.Close()',會發生什麼? – Sinatr

+0

無法在Visual Studio中獲取this.close()用於WPF UserControl –

回答

0

我得到了答案,我已要求下面那個按鈕點擊的代碼行:

MainWindow mainWind = Application.Current.MainWindow as MainWindow; 
mainWind.MainMdiContainer.Children.RemoveAt(0); 

謝謝大家的幫助。

0

我不知道這個框架,但我懷疑MDI孩子不是Window。當您致電Window.GetWindow(this).Close();時,請關閉主窗口並因此終止應用程序。

嘗試從MainMdiContainer刪除MdiChild

var child = new MdiChild() { ... }; 
MainMdiContainer.Children.Add(child); 
// Later 
MainMdiContainer.Children.Remove(child); 
+0

它是.Net 4.0 Framework,並且在MDI容器上添加子項位於MainWindow.xaml頁面上,並且將從子窗體調用該子項的刪除,並在該表單上我無法獲得「MainMdiContainer」對象來刪除子項 –

+0

我沒有談論.NET,而是外部MDI框架/庫。無論如何。爲什麼你無法訪問它?只是公開。或者在主窗口或子視圖中提供一些公共方法或視圖模型中的命令。這不是一個關於MDI的問題,而是如何使不同的UI部件通信。 –

+0

塞巴斯蒂安要求你一次通過我的問題鏈接,你會得到有關MDI框架和MDI實現方法的完整信息。它不是winForm MDI,它的WPF頁面作爲父MDI和WPF用戶控件作爲子頁面。 –

相關問題