2014-09-29 195 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