具有折叠/展开功能的控件的树视图
本文关键字:控件 视图 功能 折叠 | 更新日期: 2023-09-27 18:34:31
平台:C# Windows。.Net Framework 3.5, 4.0.我有一些控件,我想显示为树视图。我已将控件集添加到树视图,如下所示:
Control control;
.................
.................
Treeview1.Controls.Add(control);
它向我展示了树视图等面板中的控件。它不显示任何层次结构或加/减号。所以我无法折叠/展开树节点。请你给我一个解决方法。
@Banketeshvar可以使用
"流布局"面板或 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 提供了一些附加属性。