平台:C#窗口。 .Net Framework 3.5,4.0。 我有一些我想要显示为treeview的控件。 我已经将这组控件添加到treeview中,如下所示:
Control control; ................. ................. Treeview1.Controls.Add(control);
它显示了我像在面板树视图中的控制。 它没有显示任何层次结构或加号/减号。 所以我无法折叠/展开树节点。 请你给我一个解决的办法。
@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; }
如果不在运行时添加控件,则可以使用简单的Panel Control而不是FlowLayoutPanelControl。
您也可以使用普通的Panel Control进行动态控制,但FlowLayoutPanel提供了一些附加属性。