2014-09-29 60 views
0

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

经过大量寻找解决我的问题,我刚刚发现这个简单的代码,只有一个控件添加到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); 
    } 

在此代码扩展器只托管一个控件。

我应该如何定制它来托管多个控件? 请帮助

回答

1

使用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); 

} 
+0

非常感谢。 @Alexey – 2014-10-02 05:20:44