2013-10-09 2 views
1

플랫폼 : C# windows. .Net Framework 3.5, 4.0. treeview로 표시하려는 컨트롤 집합이 있습니다. 다음과 같이 컨트롤 세트를 treeview에 추가했습니다.붕괴/확장 기능이있는 Treeview

Control control; 

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

Treeview1.Controls.Add(control); 

컨트롤이 트리보기와 같이 나에게 표시됩니다. 계층 또는 플러스/마이너스 기호가 표시되지 않습니다. 따라서 트리 노드를 축소/확장 할 수 없습니다. 해결 방법을 제안 해주세요.

+2

TreeView는'Control's가 아닌'Node'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에는 몇 가지 추가 기능 속성이 있습니다.