[英]Edit active WPF control in an active window
我有一個帶有WindowsFormsHost對象的WPF窗口。 理想情況下,我希望在窗口處於活動狀態時訪問WindowsForm控件。
我可以注冊一個名稱或類似WindowsFormsHost控件所需的名稱,然后執行以下操作:
WindowCollection mainWin = System.Windows.Application.Current.Windows;
在這種特殊情況下,我知道我需要的窗口將始終位於mainWin [2]中,但是我不確定如何直接訪問WindowsFormsHost對象以向其中添加Winform控件。
偽代碼,則需要一個具有公共屬性的MyWindow類,該類在此窗口中以MyWindowsFormsHost的名稱公開WindowsFormsHost Controll。
WindowCollection mainWin = System.Windows.Application.Current.Windows;
MyWindow win = (MyWindow)mainWin[2];
win.MyWindowsFormsHost.Child = MyNewControl;
我想這就是您要實現的目標:
XAML
<WindowsFormsHost Name="host"></WindowsFormsHost>
碼
private void MainWindow_OnLoaded(object sender, RoutedEventArgs e)
{
var txb = new System.Windows.Forms.TextBox();
host.Child = txb ;
host.Child = new System.Windows.Forms.DataGrid();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.