TreeViewList控件。
属性有2个:
treeViewData:树的内容,类型是自定义的实体结构如下:
public int Id { get; set; } /// <summary> /// 父节点,空的时候为根节点 /// </summary> public int? FatherId { get; set; } public string Text { get; set; }
SelectId:选中的节点。
事件有1个:
SelectIdChanged:选择Id变化触发事件
目前该控件使用了DEV控件里面的控件,这边先暂时不考虑,之后会做去DEV控件化。
源码开放如下:
private List<TreeViewEntity> treeViewData; private Dictionary<string, string> controlPosition; public List<TreeViewEntity> TreeViewData { get { return treeViewData; } set { treeViewData = value; } } private int selectId; public int SelectId { get { return selectId; } set { selectId = value; } } public int ExpandId = 0; public TreeViewList() { InitializeComponent(); InitData(); } //定义事件 public delegate void SelectIdChangedDelegate(object sender, EventArgs e); /// <summary> /// 选择Id变化触发事件 /// </summary> public event SelectIdChangedDelegate SelectIdChanged; /// <summary> /// 初始化 /// </summary> public void InitData() { if (treeViewData == null) return; controlPosition = new Dictionary<string, string>(); Font rootFont = new Font("微软雅黑", 10.5f, FontStyle.Regular); Color rootColor = Color.FromArgb(119, 119, 119); int topPosition = 10; int rootInterval = 18; int rootLeft = 0; int index = 0; ExpandButton clickExpandBtn = new ExpandButton(); foreach (var item in treeViewData) { if (item.FatherId == null) { LabelControl rootLab = new LabelControl(); rootLab.Name = "rootLab" + index; rootLab.Tag = item.Id.ToString(); rootLab.Font = rootFont; rootLab.ForeColor = rootColor; rootLab.Location = new Point(rootLeft, topPosition + rootInterval * index + rootLab.Height * index); rootLab.Text = item.Text; ExpandButton expandBtn = new ExpandButton(); expandBtn.Name = "expandBtn" + index; expandBtn.Tag = item.Id.ToString(); expandBtn.IsExpand = false; expandBtn.BorderStyle = BorderStyle.None; expandBtn.Size = new Size(10, 10); expandBtn.Location = new Point(panelMain.Width - 15, rootLab.Location.Y + 5); expandBtn.BackColor = this.panelMain.BackColor; expandBtn.expandClickEvent += expandBtn_ExpandClickEvent; panelMain.Controls.Add(rootLab); panelMain.Controls.Add(expandBtn); controlPosition.Add(item.Id.ToString(), rootLab.Location.X + "," + rootLab.Location.Y + "|" + expandBtn.Location.X + "," + expandBtn.Location.Y); if(index==0) { clickExpandBtn=expandBtn; } } index++; } clickExpandBtn.IsExpand = true; ExpandFirst(clickExpandBtn); } private void ExpandFirst(ExpandButton expandBtn) { Font childFont = new Font("微软雅黑", 9, FontStyle.Regular); Font childSelectFont = new Font("微软雅黑", 9, FontStyle.Bold); Color childSelectColor = Color.FromArgb(184, 39, 18); Color childColor = Color.FromArgb(119, 119, 119); //展开子节点个数 int childTreeCount = treeViewData.Where(m => m.FatherId != null).Where(s => s.FatherId.Value == Convert.ToInt32(expandBtn.Tag)).Count(); if (childTreeCount <= 0) return; if (ExpandId == Convert.ToInt32(expandBtn.Tag)) { ExpandId = 0; return; } int childInterval = 5; int childTextHeight = 15; int addHeight = childTextHeight * childTreeCount + childInterval * (childTreeCount - 1); //下移其他节点 foreach (Control childLab in expandBtn.Parent.Controls) { if (Convert.ToInt32(childLab.Tag.ToString()) > Convert.ToInt32(expandBtn.Tag)) { childLab.Location = new Point(childLab.Location.X, childLab.Location.Y + addHeight); } } int childLeft = 20; int topPosition = expandBtn.Location.Y + expandBtn.Height + 10; int index = 0; //展开树 foreach (var item in treeViewData) { if (item.FatherId == Convert.ToInt32(expandBtn.Tag)) { LabelControl childLab = new LabelControl(); childLab.Name = "childLab" + index; childLab.Tag = item.Id.ToString(); childLab.Location = new Point(childLeft, topPosition + childInterval * index + childLab.Height * index); childLab.Text = item.Text; childLab.Click += childLab_Click; if (index == 0) { childLab.ForeColor = childSelectColor; childLab.Font = childSelectFont; Circle circle = new Circle(); circle.Name = "childLabCircle" + childLab.Tag.ToString(); circle.Location = new Point(childLab.Location.X - 13, childLab.Location.Y + 4); panelMain.Controls.Add(circle); selectId = item.Id; } else { childLab.Font = childFont; childLab.ForeColor = childColor; } panelMain.Controls.Add(childLab); index++; } } //展开的节点 ExpandId = Convert.ToInt32(expandBtn.Tag); } void expandBtn_ExpandClickEvent(object sender, EventArgs e) { ExpandButton expandBtn = (ExpandButton)sender as ExpandButton; Font childFont = new Font("微软雅黑", 9, FontStyle.Regular); Font childSelectFont = new Font("微软雅黑", 9, FontStyle.Bold); Color childSelectColor = Color.FromArgb(184, 39, 18); Color childColor = Color.FromArgb(119, 119, 119); if (ExpandId != 0) { //有子节点的收起来 string names = string.Empty; List<Control> removeControls = new List<Control>(); foreach (Control childLab in expandBtn.Parent.Controls) { names += childLab.Name + ","; if (childLab.Name.Contains("childLab")) { removeControls.Add(childLab); } if (childLab.Name.Contains("expandBtn")) { ExpandButton exBtn = (ExpandButton)childLab as ExpandButton; if (Convert.ToInt32(expandBtn.Tag) != Convert.ToInt32(exBtn.Tag)) exBtn.IsExpand = false; } } foreach (var item in removeControls) { expandBtn.Parent.Controls.Remove(item); item.Dispose(); } //还原位置 foreach (Control childLab in expandBtn.Parent.Controls) { if (controlPosition.ContainsKey(childLab.Tag.ToString())) { string position = controlPosition[childLab.Tag.ToString()]; if (!String.IsNullOrEmpty(position)) { string[] positionList = position.Split('|'); string labPosition = positionList[0]; string btnPosition = positionList[1]; string[] labP = labPosition.Split(','); Point labPoint = new Point(Convert.ToInt32(labP[0]), Convert.ToInt32(labP[1])); string[] btnP = btnPosition.Split(','); Point btnPoint = new Point(Convert.ToInt32(btnP[0]), Convert.ToInt32(btnP[1])); if (childLab.Name.Contains("rootLab")) childLab.Location = labPoint; if (childLab.Name.Contains("expandBtn")) childLab.Location = btnPoint; } } } } ExpandFirst(expandBtn); if (SelectIdChanged != null) { SelectIdChanged(this, e); } } void childLab_Click(object sender, EventArgs e) { Font childFont = new Font("微软雅黑", 9, FontStyle.Regular); Font childSelectFont = new Font("微软雅黑", 9, FontStyle.Bold); Color childSelectColor = Color.FromArgb(184, 39, 18); Color childColor = Color.FromArgb(119, 119, 119); LabelControl childLab = (LabelControl)sender as LabelControl; foreach (Control item in childLab.Parent.Controls) { if (item.Name.Contains("childLabCircle")) { panelMain.Controls.Remove(item); } } foreach (Control item in childLab.Parent.Controls) { if (item.Name.Contains("childLab")) { if (childLab.Tag.ToString() == item.Tag.ToString()) { item.ForeColor = childSelectColor; item.Font = childSelectFont; Circle circle = new Circle(); circle.Name = "childLabCircle" + childLab.Tag.ToString(); circle.Location = new Point(item.Location.X - 13, item.Location.Y + 4); panelMain.Controls.Add(circle); } else { item.ForeColor = childColor; item.Font = childFont; } } } }
这个控件并非优化的太好,需要优化的有几点
1.切换的时候更加平滑。
2.还无法随意配置菜单内容。
3.控件去DEV化
文章评论