首先需要一个可绑定实体

[Serializable]
public class TreeNodeModel
{
private Image _nodePic;
/// <summary>
/// 设置图标
/// </summary>
public Image NodelPic
{
get { return _nodePic; }
set { _nodePic = value; }
}
/// <summary>
/// 设置名称
/// </summary>
public string NodeText { get; set; }
private List<TreeNodeModel> _nodes = new List<TreeNodeModel>();
/// <summary>
/// 子节点
/// </summary>
public List<TreeNodeModel> Nodes
{
get { return _nodes; }
set { _nodes = value; }
}
/// <summary>
/// 数据源
/// </summary>
public object DataSource { get; set; }
private Color m_NodeBackgroundColor = Color.Empty;
/// <summary>
/// 背景颜色(默认白色)
/// </summary>
public Color NodeBackgroundColor
{
get { return m_NodeBackgroundColor; }
set { m_NodeBackgroundColor = value; }
} private bool m_canExpand = true;
/// <summary>
/// 是否可展开
/// </summary>
public bool CanExpand
{
get { return m_canExpand; }
set
{
m_canExpand = value;
}
}
private int? _nodeHeight = null;
/// <summary>
/// 节点高度
/// </summary>
public int? NodeHeight
{
get { return _nodeHeight; }
set { _nodeHeight = value; }
}
private bool _isEventClick = true;
/// <summary>
/// 是否触发点击事件
/// </summary>
public bool IsEventClick
{
get { return _isEventClick; }
set { _isEventClick = value; }
} private Color nodeForeColor=Color.Empty;
/// <summary>
/// 字体颜色
/// </summary>
public Color NodeForeColor
{
get { return nodeForeColor; }
set { nodeForeColor = value; }
}
}

然后是节点控件

设计文件

 partial class UCTreeViewNode
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null; /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
} #region 组件设计器生成的代码 /// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.lblName = new Km.Controls.Static.LabelEx();
this.lblUpDownPic = new Km.Controls.Static.LabelEx();
this.panNodePic = new Km.Controls.ExPanel.PanelEx();
this.panPic = new Km.Controls.ExPanel.PanelEx();
this.panPadding = new Km.Controls.ExPanel.PanelEx();
this.panNodePic.SuspendLayout();
this.SuspendLayout();
//
// lblName
//
this.lblName.BackColor = System.Drawing.Color.Transparent;
this.lblName.Dock = System.Windows.Forms.DockStyle.Fill;
this.lblName.Font = new System.Drawing.Font("微软雅黑", 16F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)()));
this.lblName.ForeColor = System.Drawing.Color.Silver;
this.lblName.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
this.lblName.Location = new System.Drawing.Point(, );
this.lblName.Name = "lblName";
this.lblName.Size = new System.Drawing.Size(, );
this.lblName.TabIndex = ;
this.lblName.Text = "系统设置";
this.lblName.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
//
// lblUpDownPic
//
this.lblUpDownPic.Anchor = System.Windows.Forms.AnchorStyles.Right;
this.lblUpDownPic.BackColor = System.Drawing.Color.Transparent;
this.lblUpDownPic.Image = global::Km.Controls.Properties.Resources.下翻;
this.lblUpDownPic.Location = new System.Drawing.Point(, );
this.lblUpDownPic.Name = "lblUpDownPic";
this.lblUpDownPic.Size = new System.Drawing.Size(, );
this.lblUpDownPic.TabIndex = ;
//
// panNodePic
//
this.panNodePic.BackColor = System.Drawing.Color.Transparent;
this.panNodePic.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Center;
this.panNodePic.BottomborderStyleColor = System.Drawing.Color.Empty;
this.panNodePic.BottomborderStyleSize = ;
this.panNodePic.BottomborderStyleType = System.Windows.Forms.ButtonBorderStyle.None;
this.panNodePic.ContainerPanel = null;
this.panNodePic.Controls.Add(this.panPic);
this.panNodePic.Dock = System.Windows.Forms.DockStyle.Left;
this.panNodePic.IsSelected = false;
this.panNodePic.LeftborderStyleColor = System.Drawing.Color.Empty;
this.panNodePic.LeftborderStyleSize = ;
this.panNodePic.LeftborderStyleType = System.Windows.Forms.ButtonBorderStyle.None;
this.panNodePic.Location = new System.Drawing.Point(, );
this.panNodePic.Name = "panNodePic";
this.panNodePic.Radius = ;
this.panNodePic.Redraw = false;
this.panNodePic.RightborderStyleColor = System.Drawing.Color.Empty;
this.panNodePic.RightborderStyleSize = ;
this.panNodePic.RightborderStyleType = System.Windows.Forms.ButtonBorderStyle.None;
this.panNodePic.Size = new System.Drawing.Size(, );
this.panNodePic.TabIndex = ;
this.panNodePic.TopborderStyleColor = System.Drawing.Color.Empty;
this.panNodePic.TopborderStyleSize = ;
this.panNodePic.TopborderStyleType = System.Windows.Forms.ButtonBorderStyle.None;
//
// panPic
//
this.panPic.BackgroundImage = global::Km.Controls.Properties.Resources.table_cell_bg1;
this.panPic.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
this.panPic.BottomborderStyleColor = System.Drawing.Color.Empty;
this.panPic.BottomborderStyleSize = ;
this.panPic.BottomborderStyleType = System.Windows.Forms.ButtonBorderStyle.None;
this.panPic.ContainerPanel = null;
this.panPic.IsSelected = false;
this.panPic.LeftborderStyleColor = System.Drawing.Color.Empty;
this.panPic.LeftborderStyleSize = ;
this.panPic.LeftborderStyleType = System.Windows.Forms.ButtonBorderStyle.None;
this.panPic.Location = new System.Drawing.Point(, );
this.panPic.Name = "panPic";
this.panPic.Radius = ;
this.panPic.Redraw = false;
this.panPic.RightborderStyleColor = System.Drawing.Color.Empty;
this.panPic.RightborderStyleSize = ;
this.panPic.RightborderStyleType = System.Windows.Forms.ButtonBorderStyle.None;
this.panPic.Size = new System.Drawing.Size(, );
this.panPic.TabIndex = ;
this.panPic.TopborderStyleColor = System.Drawing.Color.Empty;
this.panPic.TopborderStyleSize = ;
this.panPic.TopborderStyleType = System.Windows.Forms.ButtonBorderStyle.None;
//
// panPadding
//
this.panPadding.BackColor = System.Drawing.Color.Transparent;
this.panPadding.BottomborderStyleColor = System.Drawing.Color.Empty;
this.panPadding.BottomborderStyleSize = ;
this.panPadding.BottomborderStyleType = System.Windows.Forms.ButtonBorderStyle.None;
this.panPadding.ContainerPanel = null;
this.panPadding.Dock = System.Windows.Forms.DockStyle.Left;
this.panPadding.IsSelected = false;
this.panPadding.LeftborderStyleColor = System.Drawing.Color.Empty;
this.panPadding.LeftborderStyleSize = ;
this.panPadding.LeftborderStyleType = System.Windows.Forms.ButtonBorderStyle.None;
this.panPadding.Location = new System.Drawing.Point(, );
this.panPadding.Name = "panPadding";
this.panPadding.Radius = ;
this.panPadding.Redraw = false;
this.panPadding.RightborderStyleColor = System.Drawing.Color.Empty;
this.panPadding.RightborderStyleSize = ;
this.panPadding.RightborderStyleType = System.Windows.Forms.ButtonBorderStyle.None;
this.panPadding.Size = new System.Drawing.Size(, );
this.panPadding.TabIndex = ;
this.panPadding.TopborderStyleColor = System.Drawing.Color.Empty;
this.panPadding.TopborderStyleSize = ;
this.panPadding.TopborderStyleType = System.Windows.Forms.ButtonBorderStyle.None;
//
// UCTreeViewNode
//
this.AutoScaleDimensions = new System.Drawing.SizeF(9F, 20F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BackColor = System.Drawing.Color.Transparent;
this.BottomborderStyleColor = System.Drawing.Color.FromArgb(((int)(((byte)()))), ((int)(((byte)()))), ((int)(((byte)()))));
this.BottomborderStyleSize = ;
this.BottomborderStyleType = System.Windows.Forms.ButtonBorderStyle.Solid;
this.Controls.Add(this.lblUpDownPic);
this.Controls.Add(this.lblName);
this.Controls.Add(this.panNodePic);
this.Controls.Add(this.panPadding);
this.Name = "UCTreeViewNode";
this.RectColor = System.Drawing.Color.Red;
this.Redraw = true;
this.Size = new System.Drawing.Size(, );
this.panNodePic.ResumeLayout(false);
this.ResumeLayout(false); } #endregion private Static.LabelEx lblName;
private Static.LabelEx lblUpDownPic;
private ExPanel.PanelEx panNodePic;
private ExPanel.PanelEx panPic;
private ExPanel.PanelEx panPadding;
}

代码文件

 public partial class UCTreeViewNode : UCBase
{
#region 属性
private int _nodeHeight = ;
/// <summary>
/// 节点高度
/// </summary>
public int NodeHeight
{
get { return _nodeHeight; }
set { _nodeHeight = value; }
}
private bool _isBrotherSingleExpanded = true;
/// <summary>
/// 兄弟节点是否仅有一个为展开状态
/// </summary>
public bool IsBrotherSingleExpanded
{
get { return _isBrotherSingleExpanded; }
set { _isBrotherSingleExpanded = value; }
} private Image _downPic = Properties.Resources.下翻;
/// <summary>
/// 下翻图标
/// </summary>
public Image DownPic
{
get { return _downPic; }
set
{
_downPic = value;
lblUpDownPic.Image = value;
}
} private Image _upPic = Properties.Resources.上翻;
/// <summary>
/// 上翻图标
/// </summary>
public Image UpPic
{
get { return _upPic; }
set { _upPic = value; }
}
private Color m_NodeBackgroundColor = Color.White; /// <summary>
/// 背景颜色
/// </summary>
public Color NodeBackgroundColor
{
get { return m_NodeBackgroundColor; }
set
{
m_NodeBackgroundColor = value;
this.BackColor = value;
}
} private bool m_isExpanded = false;
/// <summary>
/// 是否已展开
/// </summary>
public bool IsExpanded
{
get { return m_isExpanded; }
private set { m_isExpanded = value; }
} private bool m_canExpand = false;
/// <summary>
/// 是否可展开
/// </summary>
public bool CanExpand
{
get { return m_canExpand; }
set
{
m_canExpand = value;
lblUpDownPic.Visible = value;
}
} private TreeNodeModel m_source;
/// <summary>
/// 数据源
/// </summary>
public TreeNodeModel Source
{
get { return m_source; }
set
{
m_source = value;
NodelPic = value.NodelPic;
NodeText = value.NodeText;
if (value.NodeBackgroundColor != Color.Empty)
NodeBackgroundColor = value.NodeBackgroundColor;
CanExpand = value.CanExpand;
if (value.Nodes.Count > )
{
lblUpDownPic.Visible = true;
}
else
{
lblUpDownPic.Visible = false;
}
}
}
private string m_nodeText;
/// <summary>
/// 标题文字
/// </summary>
public string NodeText
{
get { return m_nodeText; }
set
{
m_nodeText = value;
lblName.Text = value;
}
} private int m_nodeLeftPadding = ;
/// <summary>
///
/// </summary>
public int NodeLeftPadding
{
get { return m_nodeLeftPadding; }
set
{
m_nodeLeftPadding = value;
panPadding.Width = value;
}
} private Image _nodePic;
/// <summary>
/// 设置图标
/// </summary>
public Image NodelPic
{
get { return _nodePic; }
set
{
_nodePic = value;
panPic.BackgroundImage = value;
}
}
private List<UCTreeViewNode> m_nodes = new List<UCTreeViewNode>();
/// <summary>
/// 子节点
/// </summary>
public List<UCTreeViewNode> Nodes
{
get { return m_nodes; }
set { m_nodes = value; }
} private UCTreeViewNode m_parentNode;
/// <summary>
/// 父节点
/// </summary>
public UCTreeViewNode ParentNode
{
get { return m_parentNode; }
set { m_parentNode = value; }
} private Color _nodeForeColor = Color.Silver;
/// <summary>
/// 字体颜色
/// </summary>
public Color NodeForeColor
{
get { return _nodeForeColor; }
set
{
_nodeForeColor = value;
lblName.ForeColor = value;
}
} private bool _isShowSplitLine = false;
/// <summary>
/// 是否显示分割线
/// </summary>
public bool IsShowSplitLine
{
get { return _isShowSplitLine; }
set
{
_isShowSplitLine = value;
base.Redraw = value;
}
} private Color _splitLineColor= Color.FromArgb(, , );
/// <summary>
/// 分割线颜色
/// </summary>
public Color SplitLineColor
{
get { return _splitLineColor; }
set
{
_splitLineColor = value;
base.BottomborderStyleColor = value;
}
} /// <summary>
/// 点击节点事件
/// </summary>
public event EventHandler NodeMouseDown;
#endregion public UCTreeViewNode()
{
InitializeComponent();
lblName.MouseDown += Node_MouseDown;
lblUpDownPic.MouseDown += Node_MouseDown;
} void Node_MouseDown(object sender, MouseEventArgs e)
{
if (NodeMouseDown != null && Source.IsEventClick)
{
NodeMouseDown(this, e);
} Expanded(!IsExpanded);
} /// <summary>
/// 功能描述:展开节点
/// 作  者:
/// 创建日期:2018-06-26 17:53:45
/// 任务编号:
/// </summary>
public void Expanded(bool blnExpanded = true)
{
if (CanExpand)
{
if (Nodes.Count > )
{
foreach (var item in Nodes)
{
// item.Visible = blnExpanded;
if (blnExpanded)
{
item.Height = _nodeHeight;
}
else
{
item.Height = ;
item.ExpandedAll(false);
}
}
IsExpanded = blnExpanded;
if (blnExpanded)
{
lblUpDownPic.Image = UpPic;
if (_isBrotherSingleExpanded)
{
//兄弟节点仅展开一个时,关闭其他节点
foreach (var item in ParentNode.Nodes)
{
if (item == this)
continue;
item.ExpandedAll(false);
}
}
}
else
{
lblUpDownPic.Image = DownPic;
}
}
}
} /// <summary>
/// 功能描述:展开所有子节点
/// 作  者:
/// 创建日期:2018-06-26 17:53:45
/// 任务编号:
/// </summary>
public void ExpandedAll(bool blnExpanded = true)
{
if (CanExpand && (!_isBrotherSingleExpanded || !blnExpanded))
{
foreach (var item in Nodes)
{
//item.Visible = blnExpanded;
if (blnExpanded)
{
item.Height = _nodeHeight;
}
else
{
item.Height = ;
}
item.ExpandedAll(blnExpanded);
}
if (Nodes.Count > )
{
IsExpanded = blnExpanded; if (blnExpanded)
{
lblUpDownPic.Image = UpPic;
}
else
{
lblUpDownPic.Image = DownPic;
}
}
}
}
}

另外需要重写Onpaint事件

 protected override void OnPaint(PaintEventArgs e)
{
if (_redraw && !_isShowRect)
{
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
ControlPaint.DrawBorder(e.Graphics,
this.ClientRectangle,
_leftborderStyleColor,//7f9db9
_leftborderStyleSize,
_leftborderStyleType,
_topborderStyleColor,//7f9db9
_topborderStyleSize,
_topborderStyleType,
_rightborderStyleColor,//7f9db9
_rightborderStyleSize,
_rightborderStyleType,
_bottomborderStyleColor,//7f9db9
_bottomborderStyleSize,
_bottomborderStyleType);
} base.OnPaint(e);
}

下面是树控件

设计文件

 partial class UCTreeView
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null; /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
} #region 组件设计器生成的代码 /// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.panSources = new System.Windows.Forms.FlowLayoutPanel();
this.SuspendLayout();
//
// panSources
//
this.panSources.BackColor = System.Drawing.Color.Transparent;
this.panSources.Dock = System.Windows.Forms.DockStyle.Fill;
this.panSources.Location = new System.Drawing.Point(, );
this.panSources.Name = "panSources";
this.panSources.Size = new System.Drawing.Size(, );
this.panSources.TabIndex = ;
//
// UCTreeView
//
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
this.BackColor = System.Drawing.Color.White;
this.Controls.Add(this.panSources);
this.Name = "UCTreeView";
this.Size = new System.Drawing.Size(, );
this.Resize += new System.EventHandler(this.UCTreeView_Resize);
this.ResumeLayout(false); } #endregion private System.Windows.Forms.FlowLayoutPanel panSources; }

代码文件

 public partial class UCTreeView : UserControl
{
#region 属性
private bool _isBrotherSingleExpanded = true;
/// <summary>
/// 兄弟节点是否仅有一个为展开状态
/// </summary>
[Description("兄弟节点是否仅有一个为展开状态"), Category("自定义属性")]
public bool IsBrotherSingleExpanded
{
get { return _isBrotherSingleExpanded; }
set { _isBrotherSingleExpanded = value; }
} private List<TreeNodeModel> _dataSource;
/// <summary>
/// 数据源
/// </summary>
public List<TreeNodeModel> DataSource
{
get { return _dataSource; }
set
{
_dataSource = value;
RefreshTree();
}
} private Image _nodeDownPic = Properties.Resources.下翻;
/// <summary>
/// 下翻图标
/// </summary>
[Description("下翻图标"), Category("自定义属性")]
public Image NodeDownPic
{
get { return _nodeDownPic; }
set { _nodeDownPic = value; }
} private Image _nodeUpPic = Properties.Resources.上翻;
/// <summary>
/// 上翻图标
/// </summary>
[Description("上翻图标"), Category("自定义属性")]
public Image NodeUpPic
{
get { return _nodeUpPic; }
set { _nodeUpPic = value; }
} private Color _nodeBackgroundColor = Color.White;
/// <summary>
/// 节点背景颜色(如果数据源中设置了指定节点背景颜色,则优先使用数据源中的背景颜色)
/// </summary>
[Description("节点背景颜色(如果数据源中设置了指定节点背景颜色,则优先使用数据源中的背景颜色)"), Category("自定义属性")]
public Color NodeBackgroundColor
{
get { return _nodeBackgroundColor; }
set { _nodeBackgroundColor = value; }
} private int _nodeHeight = ;
/// <summary>
/// 节点高度(如果数据源中设置了指定节点高度,则优先使用数据源中的高度)
/// </summary>
[Description("节点高度(如果数据源中设置了指定节点高度,则优先使用数据源中的高度)"), Category("自定义属性")]
public int NodeHeight
{
get { return _nodeHeight; }
set { _nodeHeight = value; }
} private int _nodeLeftPadding = ;
/// <summary>
/// 子节点与父节点间偏移距离
/// </summary>
[Description("子节点与父节点间偏移距离"), Category("自定义属性")]
public int NodeLeftPadding
{
get { return _nodeLeftPadding; }
set { _nodeLeftPadding = value; }
} private Color _nodeForeColor = Color.Silver;
/// <summary>
/// 子节点字体颜色(如果数据源中设置了指定节点字体颜色,则优先使用数据源中的字体颜色)
/// </summary>
[Description("子节点字体颜色(如果数据源中设置了指定节点字体颜色,则优先使用数据源中的字体颜色)"), Category("自定义属性")]
public Color NodeForeColor
{
get { return _nodeForeColor; }
set { _nodeForeColor = value; }
}
private bool _nodeIsShowSplitLine = false;
/// <summary>
/// 是否显示分割线
/// </summary>
[Description("节点是否显示分割线"), Category("自定义属性")]
public bool NodeIsShowSplitLine
{
get { return _nodeIsShowSplitLine; }
set
{
_nodeIsShowSplitLine = value;
}
} private Color _nodeSplitLineColor = Color.FromArgb(, , );
/// <summary>
/// 分割线颜色
/// </summary>
[Description("节点分割线颜色"), Category("自定义属性")]
public Color NodeSplitLineColor
{
get { return _nodeSplitLineColor; }
set
{
_nodeSplitLineColor = value;
}
}
#endregion /// <summary>
/// 节点点击事件
/// </summary>
[Description("节点点击事件"), Category("自定义")]
public event EventHandler NodeMouseDown; /// <summary>
/// 功能描述:构造函数
/// 作  者:
/// 创建日期:2018-06-26 16:58:51
/// 任务编号:
/// </summary>
public UCTreeView()
{
InitializeComponent();
} /// <summary>
/// 功能描述:刷新列表
/// 作  者:
/// 创建日期:2018-06-26 16:58:39
/// 任务编号:
/// </summary>
public void RefreshTree()
{
this.panSources.Controls.Clear();
if (_dataSource == null || _dataSource.Count <= )
{
return;
}
int intTabIndex = ;
UCTreeViewNode nodeBase = new UCTreeViewNode();
nodeBase.Tag = ;
LoadNode(nodeBase, _dataSource, ref intTabIndex, true);
} /// <summary>
/// 功能描述:加载节点
/// 作  者:
/// 创建日期:2018-06-27 09:37:45
/// 任务编号:
/// </summary>
/// <param name="parentNode">parentNode</param>
/// <param name="lstNodes">lstNodes</param>
/// <param name="intTabIndex">intTabIndex</param>
/// <param name="blnVisible">blnVisible</param>
private void LoadNode(
UCTreeViewNode parentNode,
List<TreeNodeModel> lstNodes,
ref int intTabIndex,
bool blnVisible = false)
{
for (int i = ; i < lstNodes.Count; i++)
{
var item = lstNodes[i];
UCTreeViewNode node = new UCTreeViewNode();
node.IsShowSplitLine = _nodeIsShowSplitLine;
node.SplitLineColor = _nodeSplitLineColor;
node.Tag = parentNode.Tag.ToIntExt() + ;
node.ParentNode = parentNode;
node.UpPic = _nodeUpPic;
node.DownPic = _nodeDownPic;
node.Margin = new System.Windows.Forms.Padding();
node.NodeLeftPadding = _nodeLeftPadding * parentNode.Tag.ToIntExt();
node.NodeMouseDown += node_NodeMouseDown;
node.Anchor = AnchorStyles.Left | AnchorStyles.Right;
node.Size = new System.Drawing.Size(this.Width, blnVisible ? (item.NodeHeight.HasValue ? item.NodeHeight.Value : _nodeHeight) : );
node.NodeBackgroundColor = _nodeBackgroundColor;
node.NodeForeColor = item.NodeForeColor == Color.Empty ? _nodeForeColor : item.NodeForeColor; node.TabIndex = ++intTabIndex;
node.IsBrotherSingleExpanded = _isBrotherSingleExpanded;
node.NodeHeight = (item.NodeHeight.HasValue ? item.NodeHeight.Value : _nodeHeight);
parentNode.Nodes.Add(node);
this.panSources.Controls.Add(node);
Console.WriteLine(item.NodeText);
if (item.Nodes != null && item.Nodes.Count > )
{
LoadNode(node, item.Nodes, ref intTabIndex, false);
}
node.Source = item;
}
} /// <summary>
/// 功能描述:节点点击事件
/// 作  者:
/// 创建日期:2018-06-27 09:31:44
/// 任务编号:
/// </summary>
/// <param name="sender">sender</param>
/// <param name="e">e</param>
void node_NodeMouseDown(object sender, EventArgs e)
{
if (NodeMouseDown != null)
{
NodeMouseDown(sender, e);
}
} /// <summary>
/// 改变大小时改变子项宽度,防止流控件横向显示多个
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void UCTreeView_Resize(object sender, EventArgs e)
{
this.SuspendLayout();
for (int i = this.panSources.Controls.Count-; i >=; i--)
{
this.panSources.Controls[i].Width = this.Width;
}
this.ResumeLayout(false);
} }

代码中用到了其他一些自定义控件,替换为常规控件即可

调用如下

 private void TestTreeView_Load(object sender, EventArgs e)
{
List<TreeNodeModel> lst = new List<TreeNodeModel>();
for (int i = ; i < ; i++)
{
TreeNodeModel node = new TreeNodeModel()
{
CanExpand = true,
DataSource = i,
NodeBackgroundColor = Color.FromArgb(, , ),
NodeText = string.Format("节点{0}", i),
NodelPic = Properties.Resources.Exit
};
for (int j = ; j < ; j++)
{
TreeNodeModel node2 = new TreeNodeModel()
{
CanExpand = true,
DataSource = j,
NodeForeColor = Color.Black,
NodelPic = Properties.Resources.add,
NodeText = string.Format("节点{0}-{1}", i, j)
};
for (int k = ; k < ; k++)
{
TreeNodeModel node3 = new TreeNodeModel()
{
CanExpand = true,
DataSource = k,
NodeForeColor = Color.Black,
NodeText = string.Format("节点{0}-{1}-{2}", i, j, k)
};
node2.Nodes.Add(node3);
}
node.Nodes.Add(node2);
}
lst.Add(node);
}
this.ucTreeView1.DataSource = lst;
} private void ucTreeView1_NodeMouseDown(object sender, EventArgs e)
{
UCTreeViewNode node = sender as UCTreeViewNode;
TreeNodeModel model = node.Source;
}

支持设置每个节点颜色,字体颜色,图标,展开关闭图标,是否显示分割线及颜色

效果如下

最新文章

  1. r-cnn学习(四):train_faster_rcnn_alt_opt.py源码学习
  2. Python之路-(js正则表达式、前端页面的模板套用、Django基础)
  3. Android 中dp和px
  4. Mina、Netty、Twisted一起学(五):整合protobuf
  5. Spring aop报错:com.sun.proxy.$Proxy cannot be cast to xxx
  6. ZT 第一范式,第二范式,第三范式
  7. iOS程序的启动过程-UIWindow
  8. VIM的配置文件(vimrc)在哪里?【Win7】
  9. java中的类集框架
  10. PHP设计模式笔记二:面向对象 -- Rango韩老师 http://www.imooc.com/learn/236
  11. Ubuntu14.04LST安装weblogic11g
  12. 使用 IObjectSafety 标记 ATL 控件初始化的安全
  13. DL4NLP —— seq2seq+attention机制的应用:文档自动摘要(Automatic Text Summarization)
  14. ACM1007
  15. .net core iis配置
  16. 多功能版vue日历控件
  17. Lodop打印控件 如何打印虚线
  18. Codeforces 346D Robot Control(01BFS)
  19. codeblocks: 使用静态(static)链接库(pcre)的配置
  20. composer 使用

热门文章

  1. QQ登录, 腾讯开放平台和QQ互联的坑
  2. Java反射xml数据类
  3. spring boot jar包 linux 部署
  4. WIN10+QT5.9+VS2015编译RedisDesktopManager
  5. echarts的一些基础笔记
  6. Android 调用系统分享文字、图片、文件,可直达微信、朋友圈、QQ、QQ空间、微博
  7. Oracle 11g系统分区表中的新功能
  8. Cocos2d-x移植WP8时间CCScrollView问题
  9. Android面HTTP协议发送get要求
  10. Myeclipse2014 激活 (包括方法和工具)