使用DEV控件也有一段时间了,一直想写点东西。最近又使用到TreeList控件,这个控件对于刚使用的人来说确实不好掌握。我想把自己知道的写下来,让还不熟悉的慢慢学会使用,对于会使用的大家交流下。如果有用的不正确或不恰当的欢迎各位指正批评。
一,第一步从控件库里拖出TreeList
二,设置属性
this.treeList1.OptionsView.ShowCheckBoxes = true;
this.treeList1.OptionsView.ShowColumns = false;
this.treeList1.OptionsView.ShowHorzLines = false;
this.treeList1.OptionsView.ShowIndicator = false;
this.treeList1.OptionsView.ShowVertLines = false;
三,构造测试数据(参考方法 FrmTreeList_Load)
四,设置选中项(参考方法 SetCheckState )
五,当前操作的节点为某个节点的最后一个子节点时,设置父节点的状态
这里需要一个 事件即:treeList1_AfterCheckNode 这个方法在属性事件里找到。
页面所有代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraTreeList.Nodes; namespace WinForm2
{
public partial class FrmTreeList : DevExpress.XtraEditors.XtraForm
{
public FrmTreeList()
{
InitializeComponent();
} private DataTable dt = new DataTable();
/// <summary>
/// 要被选中的节点
/// </summary>
public List<string> SelectItem = new List<string>(); //初始化方法加载测试数据
private void FrmTreeList_Load(object sender, EventArgs e)
{
dt.Columns.Add("ID", typeof(int));
dt.Columns.Add("ItemName", typeof(string));
dt.Columns.Add("ParentID", typeof(int)); DataRow dr = dt.NewRow();
dr["ID"] = ;
dr["ItemName"] = "根节点";
dr["ParentID"] = -;
dt.Rows.Add(dr); DataRow dr2 = dt.NewRow();
dr2["Id"] = ;
dr2["ItemName"] = "根节点2";
dr2["ParentID"] = ;
dt.Rows.Add(dr2); DataRow dr3 = dt.NewRow();
dr3["ID"] = ;
dr3["ItemName"] = "根节点3";
dr3["ParentID"] = ;
dt.Rows.Add(dr3); DataRow dr4 = dt.NewRow();
dr4["ID"] = ;
dr4["ItemName"] = "子节点1";
dr4["ParentID"] = ;
dt.Rows.Add(dr4); DataRow dr5 = dt.NewRow();
dr5["ID"] = ;
dr5["ItemName"] = "子节点2";
dr5["ParentID"] = ;
dt.Rows.Add(dr5); DataRow dr6 = dt.NewRow();
dr6["ID"] = ;
dr6["ItemName"] = "子子节点1";
dr6["ParentID"] = ;
dt.Rows.Add(dr6); DataRow dr7 = dt.NewRow();
dr7["ID"] = ;
dr7["ItemName"] = "子子节点2";
dr7["ParentID"] = ;
dt.Rows.Add(dr7); this.treeList1.DataSource = dt;
this.treeList1.KeyFieldName = "ID";
this.treeList1.ParentFieldName = "ParentID"; //这里是用来测试的节点
SelectItem.Add("子子节点1");
SelectItem.Add("根节点2");
// 设置初始状态
SetCheckState(SelectItem, "ItemName"); } #region 按配置选中相关节点项 //设置节点的选中状态
private void SetCheckState(List<string> list, string fieldName)
{
foreach (TreeListNode n in treeList1.Nodes)
{
DataRowView drv = (DataRowView)treeList1.GetDataRecordByNode(n);
string nodeName = (drv != null && drv[fieldName] != null) ? Convert.ToString(drv[fieldName]) : string.Empty;
if (list.Contains(nodeName))
{
n.CheckState = CheckState.Checked;
}
//如果当前结点有子结点,再枚举所有子结点
if (n.HasChildren)
this.SelectedChildNode(n, fieldName);
} } /// <summary>
/// 设置子节点状态
/// </summary>
/// <param name="parentNode"></param>
private void SelectedChildNode(TreeListNode parentNode, string fieldName)
{
foreach (TreeListNode n in parentNode.Nodes)
{
DataRowView drv = (DataRowView)treeList1.GetDataRecordByNode(n);
string nodeName = (drv != null && drv[fieldName] != null) ? Convert.ToString(drv[fieldName]) : string.Empty;
if (SelectItem.Contains(nodeName))
{
n.CheckState = CheckState.Checked;
// 这里是将父节点,父节点的父节点设置为选中状态
SelectedParentNode(n);
}
if (n.HasChildren) this.SelectedChildNode(n, fieldName);
}
} /// <summary>
/// 设置相关的父节点被选中
/// </summary>
/// <param name="parentNode"></param>
private void SelectedParentNode(TreeListNode parentNode)
{
if (parentNode.ParentNode != null)
{
parentNode.ParentNode.CheckState = CheckState.Checked;
SelectedParentNode(parentNode.ParentNode);
}
} #endregion #region 节点选中或取消相关处理 //节点选中后处理
private void treeList1_AfterCheckNode(object sender, DevExpress.XtraTreeList.NodeEventArgs e)
{
SetChildNodeState(e.Node, e.Node.CheckState);
SetParentNodeState(e.Node);
} //设置子节点的状态(当选中父节点时所有的子节点也要选中)
private void SetChildNodeState(TreeListNode node, CheckState state)
{ if (node.HasChildren)
{
foreach (TreeListNode item in node.Nodes)
{
item.CheckState = state;
if (item.HasChildren) SetChildNodeState(item, state);
}
}
} //检查节点是否是最后一个被选中或取消的子节点,如果是要对父节点进行设置(选中或不选中)
private void SetParentNodeState(TreeListNode node)
{
if (node.ParentNode != null)
{
// 方法一
//bool state = false;
//foreach (TreeListNode item in node.ParentNode.Nodes)
//{
// state = item.Checked;
// if (state) break;
//}
//node.ParentNode.Checked = state; // 方法二
bool state = true;
node.ParentNode.CheckState = CheckState.Checked;
foreach (TreeListNode item in node.ParentNode.Nodes)
{
if (item.CheckState != node.CheckState)
{
state = false;
break;
}
}
if (state) node.ParentNode.CheckState = node.CheckState; // 递归检查父父节点
SetParentNodeState(node.ParentNode);
}
} #endregion // 设置选中项的背景
private void treeList1_CustomDrawNodeCell(object sender, DevExpress.XtraTreeList.CustomDrawNodeCellEventArgs e)
{
if (e.Node.Checked)
{
e.Appearance.BackColor = Color.LightBlue;
}
else
{
e.Appearance.BackColor = Color.Transparent;
}
}
}
}