开发者

.Net: How to use a WPF user control in a windows forms application?

开发者 https://www.devze.com 2022-12-30 16:31 出处:网络
How to use a WPF user control in a windows 开发者_运维知识库forms application?From the MSDN: Use the ElementHost control to place a

How to use a WPF user control in a windows 开发者_运维知识库forms application?


From the MSDN:

Use the ElementHost control to place a WPF UIElement on your Windows Forms control or form.

Example:

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

Here is a nice tutorial how to do that.

The control called ElementHost is for WPF in WinForms what the WindowsFormsHost was for WinForms in WPF. In the designer, you can find this control in the Toolbox underneath "WPF Interoperability".

0

精彩评论

暂无评论...
验证码 换一张
取 消