//如果只是无层级tree,可不绑定PID
treeList1.DataSource = treeData;//数据源
treeList1.KeyFieldName = "ID"; //主键字段名称
treeList1.ParentFieldName = "PID"; //父级字段名称
//展开所有节点
treeList1.ExpandAll();
//treeList1.ExpandToLevel(0);展开节点level 展开1级菜单 --》二级菜单
开启checkbox
AllowIndeterminateCheckState TREELIST 第三种状态
OptionsView.ShowColumns = False//不显示列标题
OptionsView.ShowCheckBoxes = True //开启复选框
如果想达到选择父节点,子节点也同时选中的效果,需将treeList.OptionsBehavior.AllowRecursiveNodeChecking设置为True。
注册事件
treeList1.DoubleClick += new EventHandler(treeList_DoubleClick);//注册双击事件
private void treeList_DoubleClick(object sender, EventArgs e)
{ TreeList tree = sender as TreeList;
//var tableName = tree.FocusedNode.GetDisplayText(0);
var treeDoubleValue = tree.FocusedValue.ToString();//节点显示的NAME值
var ID= tree.FocusedNode.GetValue("列filedName").ToString();//节点绑定的字段值
}
//注册列Draw事件
treeListNavUP.CustomDrawNodeCell += new CustomDrawNodeCellEventHandler(treeListNavUP_CustomDrawNodeCell);//注册列Draw事件 可用于节点列的背景颜色设置或者checkbox勾选设置 类似的事件还有CustomColumnDisplayText 用于重写显示的Name值
private void treeListNavUP_CustomDrawNodeCell(object sender, CustomDrawNodeCellEventArgs e)
{ //TreeListNode node = sender as TreeListNode;
//checkList 为需要选中的数据 用于设置选中状态
foreach (var item in checkList)
{
if (e.Node.GetValue("ID").ToString() == item)
{
e.Node.CheckState = CheckState.Checked;
e.Node.ParentNode.CheckState = CheckState.Indeterminate;
e.Node.ParentNode.ParentNode.CheckState = CheckState.Indeterminate;
SyncNodeCheckState_Parent(e.Node, CheckState.Checked);
}
}
if (e.Node.CheckState == CheckState.Checked || e.Node.CheckState == CheckState.Indeterminate)
{
e.Appearance.BackColor = Color.LightBlue;
}
}
private void treeListNavUP_CustomColumnDisplayText(object sender, CustomColumnDisplayTextEventArgs e)
{
var ID= e.Node.GetValue("ID").ToString();//可根据ID值改变显示的NAME值
if (e.Column.FieldName == "ID") {//可根据列绑定的字段名称改变显示的NAME值
e.DisplayText = "";
}
}
private void treeListNavUP_AfterCheckNode(object sender, NodeEventArgs e)
{
SetCheckedChildNodes(e.Node, e.Node.CheckState);// 设置子节点的状态
SyncNodeCheckParentState(e.Node, e.Node.CheckState);//设置父节点的状态
}
private void treeListNavUP_BeforeCheckNode(object sender, CheckNodeEventArgs e)
{
e.State = (e.PrevState == CheckState.Checked ? CheckState.Unchecked : CheckState.Checked);
}
private static void SyncNodeCheckParentState(TreeListNode node, CheckState check)
{
if (node.ParentNode != null)
{
bool _cked = false;
CheckState _ckState;
foreach (TreeListNode cn in node.ParentNode.Nodes)
{
_ckState = cn.CheckState;
if (check != _ckState)
{
_cked = !_cked;
break;
}
}
node.ParentNode.CheckState = _cked ? CheckState.Indeterminate : check;
SyncNodeCheckParentState(node.ParentNode, check);
}
}
///
/// 设置子节点的状态
///
///
///
private void SetCheckedChildNodes(DevExpress.XtraTreeList.Nodes.TreeListNode node, CheckState check)
{
for (int i = 0; i < node.Nodes.Count; i++)
{
node.Nodes[i].CheckState = check;
SetCheckedChildNodes(node.Nodes[i], check);
}
}
private void bindusertable_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
//ToDo
}
```
7、给treelist注册单击事件
```csharp
///
/// treelist 右击弹出菜单
/// 添加
///
///
///
private void treeList1_MouseClick(object sender, MouseEventArgs e)
{
try
{
TreeList tree = sender as TreeList;
//判断是鼠标右击
if (e.Button == MouseButtons.Right
&& ModifierKeys == Keys.None
&& treeList1.State == TreeListState.Regular)
{
Point p = new Point(Cursor.Position.X, Cursor.Position.Y);//位置
TreeListHitInfo hitInfo = tree.CalcHitInfo(e.Location);
if (hitInfo.HitInfoType == HitInfoType.Cell)
{
tree.SetFocusedNode(hitInfo.Node);
}
if (tree.FocusedNode != null)
{
TreeFocusNode = tree.FocusedNode;//鼠标焦点所在的node
//获取点击菜单 的值 进行判断 不是表 就弹出菜单1 是表就弹出菜单2
string focusValue = tree.FocusedNode.GetValue(0).ToString();//点击菜单的值
string focusValueId = tree.FocusedNode.GetValue(treeId).ToString();//点击菜单的id
//根据不同条件显示不同popup
if (focusValue == "条件")
{
popupMenu3.ShowPopup(p);//新建专题
}
else
{
if (string.IsNullOrEmpty(tableName))//不是表
popupMenu1.ShowPopup(p);//绑定用户表
else
popupMenu2.ShowPopup(p);//移除分类
}
}
}
}
catch (Exception ex)
{
return;
}
}
///
/// tree 图片
///
///
///
private void TreeListGeo_GetSelectImage(object sender, DevExpress.XtraTreeList.GetSelectImageEventArgs e)
{
if (e.Node == null) return;
TreeListNode node = e.Node;
string nodeVal = node.GetValue("id").ToString();
if (node.GetDisplayText(0).ToString() == yuanshuju)
{
e.NodeImageIndex = 3;//根据 NodeImageIndex 执行图片
}
}