.Net:如何在Windows窗体应用程序中使用WPF用户控件?

odi*_*seh 1 wpf user-controls winforms

如何在Windows窗体应用程序中使用WPF用户控件?

maf*_*afu 5

来自MSDN:

使用ElementHost控件在Windows窗体控件或窗体上放置WPF UIElement.

例:

private void Form1_Load(object sender, EventArgs e)
{
    // Create the ElementHost control for hosting the
    // WPF UserControl.
    ElementHost host = new ElementHost();
    host.Dock = DockStyle.Fill;

    // Create the WPF UserControl.
    HostingWpfUserControlInWf.UserControl1 uc =
        new HostingWpfUserControlInWf.UserControl1();

    // Assign the WPF UserControl to the ElementHost control's
    // Child property.
    host.Child = uc;

    // Add the ElementHost control to the form's
    // collection of child controls.
    this.Controls.Add(host);
}
Run Code Online (Sandbox Code Playgroud)

是一个很好的教程如何做到这一点.

名为ElementHost的控件用于WinForms中的WPF,WindowsFormsHost是WPF中WinForms的用途.在设计器中,您可以在"WPF互操作性"下的工具箱中找到此控件.