2013-10-09 80 views
1

平臺:C#窗口。 .Net Framework 3.5,4.0。 我有一些我想要顯示爲treeview的控件。我已經將這組控件添加到treeview中,如下所示:控件崩潰/展開樹形視圖

Control control; 

................. 
................. 

Treeview1.Controls.Add(control); 

它向我展示了像treeview一樣的控制面板。它沒有顯示任何層次結構或加號/減號。所以我無法摺疊/展開樹節點。 請你給我一個解決方法。

+2

TreeView旨在顯示'Node's作爲分層方式而不是'Control's –

+0

張貼您需要的屏幕截圖。我很確定你不能在winforms中做你想做的事情。 –

+0

對不起,我的名聲低於10,所以我無法發佈任何圖片。 –

回答

2

@Banketeshvar你可以使用Flowlayout面板或WPF擴展器。

WPF擴展的代碼是:

<Window x:Class="WpfExpander.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="MainWindow" Height="350" Width="525"> 
<Grid VerticalAlignment="Stretch" Margin="3,3,3,3"> 

    <Grid.RowDefinitions> 
     <RowDefinition Height="0*" /> 
     <RowDefinition Height="85" /> 
     <RowDefinition Height="220" /> 
    </Grid.RowDefinitions> 

    <Expander Grid.Row="1" Header="expander1" Name="expander1" IsExpanded="False">    
      <ListBox> 
       <ListBoxItem Content="Unit 1"/> 
       <ListBoxItem Content="Unit 2"/> 
      </ListBox>   

    </Expander> 
    <Expander Grid.Row="2" Header="expander2" Name="expander2" IsExpanded="False"> 
    <ListBox> 
     <ListBoxItem Content="Unit 1"/> 
     <ListBoxItem Content="Unit 2"/> 
    </ListBox> 
    </Expander> 
</Grid> 
</Window> 

和的FlowLayout面板代碼:

public partial class Form1 : Form 
{ 
    private bool _open; 
    public Form1() 
    { 
     InitializeComponent(); 
     this.flowLayoutPanel1.ClientSize = new Size(this.panel1.ClientSize.Width,0); 

    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     if (_open) 
     { 
      this.flowLayoutPanel1.ClientSize = new Size(this.panel1.ClientSize.Width,0); 
      //this.ClientSize = new Size(this.panel1.ClientSize.Width, this.panel1.ClientSize.Height);     
      this.button1.Text = "+"; 
     } 
     else 
     { 
      //this.ClientSize = new Size(this.panel1.ClientSize.Width, this.panel1.ClientSize.Height + this.flowLayoutPanel1.PreferredSize.Height); 
      this.flowLayoutPanel1.ClientSize= new Size(this.panel1.ClientSize.Width, this.panel1.ClientSize.Height + this.flowLayoutPanel1.PreferredSize.Height); 
      this.button1.Text = "-"; 
     } 

     this._open = !this._open; 
    } 

    private void button5_Click(object sender, EventArgs e) 
    { 
     Button btn1=new Button(); 
     btn1.Text="aaaaaaaaaaaaaaaa"; 
     flowLayoutPanel1.FlowDirection = FlowDirection.TopDown; 
     flowLayoutPanel1.Height += 100; 
     flowLayoutPanel1.Controls.Add(btn1); 

    } 
    } 
} 



partial class Form1 
{ 
    /// <summary> 
    /// Required designer variable. 
    /// </summary> 
    private System.ComponentModel.IContainer components = null; 

    /// <summary> 
    /// Clean up any resources being used. 
    /// </summary> 
    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> 
    protected override void Dispose(bool disposing) 
    { 
     if (disposing && (components != null)) 
     { 
      components.Dispose(); 
     } 
     base.Dispose(disposing); 
    } 

    #region Windows Form Designer generated code 

    /// <summary> 
    /// Required method for Designer support - do not modify 
    /// the contents of this method with the code editor. 
    /// </summary> 
    private void InitializeComponent() 
    { 
     this.panel1 = new System.Windows.Forms.Panel(); 
     this.label1 = new System.Windows.Forms.Label(); 
     this.button1 = new System.Windows.Forms.Button(); 
     this.flowLayoutPanel1 = new System.Windows.Forms.FlowLayoutPanel(); 
     this.button5 = new System.Windows.Forms.Button(); 
     this.panel1.SuspendLayout(); 
     this.SuspendLayout(); 
     // 
     // panel1 
     // 
     this.panel1.Controls.Add(this.label1); 
     this.panel1.Controls.Add(this.button1); 
     this.panel1.Location = new System.Drawing.Point(0, 0); 
     this.panel1.Name = "panel1"; 
     this.panel1.Size = new System.Drawing.Size(200, 30); 
     this.panel1.TabIndex = 0; 
     // 
     // label1 
     // 
     this.label1.AutoSize = true; 
     this.label1.Location = new System.Drawing.Point(127, 9); 
     this.label1.Name = "label1"; 
     this.label1.Size = new System.Drawing.Size(35, 13); 
     this.label1.TabIndex = 1; 
     this.label1.Text = "label1"; 
     // 
     // button1 
     // 
     this.button1.Location = new System.Drawing.Point(4, 4); 
     this.button1.Name = "button1"; 
     this.button1.Size = new System.Drawing.Size(75, 23); 
     this.button1.TabIndex = 0; 
     this.button1.Text = "button1"; 
     this.button1.UseVisualStyleBackColor = true; 
     this.button1.Click += new System.EventHandler(this.button1_Click); 
     // 
     // flowLayoutPanel1 
     // 
     this.flowLayoutPanel1.BackColor = System.Drawing.Color.SandyBrown; 
     this.flowLayoutPanel1.Location = new System.Drawing.Point(4, 36); 
     this.flowLayoutPanel1.Name = "flowLayoutPanel1"; 
     this.flowLayoutPanel1.Size = new System.Drawing.Size(553, 289); 
     this.flowLayoutPanel1.TabIndex = 1; 
     // 
     // button5 
     // 
     this.button5.Location = new System.Drawing.Point(38, 389); 
     this.button5.Name = "button5"; 
     this.button5.Size = new System.Drawing.Size(75, 23); 
     this.button5.TabIndex = 2; 
     this.button5.Text = "Add Buttons"; 
     this.button5.UseVisualStyleBackColor = true; 
     this.button5.Click += new System.EventHandler(this.button5_Click); 
     // 
     // Form1 
     // 
     this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); 
     this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 
     this.ClientSize = new System.Drawing.Size(840, 453); 
     this.Controls.Add(this.button5); 
     this.Controls.Add(this.flowLayoutPanel1); 
     this.Controls.Add(this.panel1); 
     this.Name = "Form1"; 
     this.Text = "Form1"; 
     this.panel1.ResumeLayout(false); 
     this.panel1.PerformLayout(); 
     this.ResumeLayout(false); 

    } 

    #endregion 

    private System.Windows.Forms.Panel panel1; 
    private System.Windows.Forms.Label label1; 
    private System.Windows.Forms.Button button1; 
    private System.Windows.Forms.FlowLayoutPanel flowLayoutPanel1; 
    private System.Windows.Forms.Button button5; 
} 

如果您不添加在運行時的控制,那麼你可以使用簡單的面板控制,而不是FlowLayoutPanelControl。

您也可以將普通面板控件用於動態控件,但FlowLayoutPanel提供了一些附加屬性。