简体   繁体   中英

Edit active WPF control in an active window

I have a WPF window with a WindowsFormsHost object inside it. Ideally, I would like to access the WindowsForm control while the window is active.

I can register a name or anything like that I would need for the WindowsFormsHost control, and then do something along the lines of:

            WindowCollection mainWin = System.Windows.Application.Current.Windows;

In this particular case I know the window I need will always be in mainWin[2], but I'm not sure how I can access the WindowsFormsHost object directly to add a Winform control to it.

pseudo code, you will need a MyWindow class with a public property exposing the WindowsFormsHost Controll in this window with the name of MyWindowsFormsHost.

WindowCollection mainWin = System.Windows.Application.Current.Windows;
MyWindow win = (MyWindow)mainWin[2];
win.MyWindowsFormsHost.Child = MyNewControl;

I guess this is what you want to acheive:

XAML

    <WindowsFormsHost Name="host"></WindowsFormsHost>

Code

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();
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM