在带有多个控件的Winforms中使用WPF扩展器

本文关键字:WPF 扩展器 Winforms 控件 | 更新日期: 2023-09-27 18:24:55

我的问题是我想使用WPF扩展器对象来托管一些winforms控件。我要使用的位置是在我的应用程序的设置表单中。但是,我找不到的是添加多个控件。

在为我的问题寻找了很多解决方案后,我发现了这个简单的代码,它只向WPF扩展器对象添加了一个控件(我需要添加多个控件):

private void Form1_Load(object sender, EventArgs e)
    {
        System.Windows.Controls.Expander expander = new System.Windows.Controls.Expander();
        expander.Header = "Sample";
        WPFHost = new ElementHost();
        WPFHost.Dock = DockStyle.Fill;
        WindowsFormsHost host = new WindowsFormsHost();
        host.Child = new DateTimePicker();
        expander.Content = host;
        WPFHost.Child = expander;
        this.Controls.Add(WPFHost);
    }

在此代码中,扩展器仅承载一个控件。

我应该如何自定义它以承载多个控件?请帮助

在带有多个控件的Winforms中使用WPF扩展器

使用System.Windows.Forms.Panel作为容器将有所帮助:

private void Form1_Load(object sender, EventArgs e)
{
    System.Windows.Controls.Expander expander = new System.Windows.Controls.Expander();
    System.Windows.Controls.Grid grid = new System.Windows.Controls.Grid();
    expander.Header = "Sample";
    ElementHost WPFHost = new ElementHost();
    WPFHost.Dock = DockStyle.Fill;
    Panel panel1 = new Panel();
    DateTimePicker dtPicker1 = new DateTimePicker();
    Label label1 = new Label();

    // Initialize the Label and TextBox controls.
    label1.Location = new System.Drawing.Point(16, 16);
    label1.Text = "Select a date:";
    label1.Size = new System.Drawing.Size(104, 16);
    dtPicker1.Location = new System.Drawing.Point(16, 32);
    dtPicker1.Text = "";
    dtPicker1.Size = new System.Drawing.Size(152, 20);
    // Add the Panel control to the form. 
    this.Controls.Add(panel1);
    // Add the Label and TextBox controls to the Panel.
    panel1.Controls.Add(label1);
    panel1.Controls.Add(dtPicker1);

    WindowsFormsHost host = new WindowsFormsHost();
    host.Child = panel1;

    expander.Content = host;
    WPFHost.Child = expander;
    this.Controls.Add(WPFHost);
}