WindowsFormsHost.Child 属性

定义

获取或设置由 WindowsFormsHost 元素托管的子控件。

C#
public System.Windows.Forms.Control Child { get; set; }

属性值

托管的 Windows 窗体控件。

例外

尝试将顶级窗体分配为托管控件。

示例

下面的代码示例演示如何使用 Child 属性将托管控件添加到 WindowsFormsHost 元素。 有关详细信息,请参阅 演练:在 WPF中托管 ActiveX 控件。

C#
private void Window_Loaded(object sender, RoutedEventArgs e) 
{
    // Create the interop host control.
    System.Windows.Forms.Integration.WindowsFormsHost host =
        new System.Windows.Forms.Integration.WindowsFormsHost();

    // Create the ActiveX control.
    var axWmp = new WmpAxLib.AxWindowsMediaPlayer();

    // Assign the ActiveX control as the host control's child.
    host.Child = axWmp;

    // Add the interop host control to the Grid
    // control's collection of child controls.
    this.grid1.Children.Add(host);

    // Play a .wav file with the ActiveX control.
    axWmp.URL = @"C:\Windows\Media\tada.wav";
}

注解

只能托管一个子控件,但 Child 可以具有任意数量的子控件。

子控件不能是顶级窗体。

如果 ChildForm,则窗体的 ControlBox 属性设置为 false

XAML 文本用法

< 对象>

< hostedControl.../>

</ 对象>

XAML 值

对象 A WindowsFormsHost 元素(或可能的子类)。

hostedControl 指定托管 Windows 窗体控件的元素。 在标记中指定自定义控件通常需要 xmlns 映射,请参阅 WPF XAML 的 XAML 命名空间和命名空间映射

适用于

产品 版本
.NET Framework 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

另请参阅