简体   繁体   English

在活动窗口中编辑活动WPF控件

[英]Edit active WPF control in an active window

I have a WPF window with a WindowsFormsHost object inside it. 我有一个带有WindowsFormsHost对象的WPF窗口。 Ideally, I would like to access the WindowsForm control while the window is active. 理想情况下,我希望在窗口处于活动状态时访问WindowsForm控件。

I can register a name or anything like that I would need for the WindowsFormsHost control, and then do something along the lines of: 我可以注册一个名称或类似WindowsFormsHost控件所需的名称,然后执行以下操作:

            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. 在这种特殊情况下,我知道我需要的窗口将始终位于mainWin [2]中,但是我不确定如何直接访问WindowsFormsHost对象以向其中添加Winform控件。

pseudo code, you will need a MyWindow class with a public property exposing the WindowsFormsHost Controll in this window with the name of MyWindowsFormsHost. 伪代码,则需要一个具有公共属性的MyWindow类,该类在此窗口中以MyWindowsFormsHost的名称公开WindowsFormsHost Controll。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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