常用方法:
复选框的操作( 复选框选中状态改变时,改变相应的父节点和子节点 的选中状态)
获取所有选中状态的数据的主键集合
一些常用的属性
1. 复选框的操作
显示复选框 ShowCheckBoxes属性设为True, 有用到BeforeCheckNode和AfterCheckNode 事件
#region 复选框的操作
private void treeNavigation_AfterCheckNode(object sender, NodeEventArgs e)
{
SetCheckedChildNodes(e.Node, e.Node.CheckState);
SetCheckedParentNodes(e.Node, e.Node.CheckState);
}
private void treeNavigation_BeforeCheckNode(object sender, CheckNodeEventArgs e)
{
e.State = (e.PrevState == CheckState.Checked ? CheckState.Unchecked : CheckState.Checked);
}
/// <summary>
/// 设置子节点的状态
/// </summary>
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);
}
}
/// <summary>
/// 设置父节点的状态
/// </summary>
private void SetCheckedParentNodes(DevExpress.XtraTreeList.Nodes.TreeListNode node, CheckState check)
{
if (node.ParentNode != null)
{
bool b = false;
CheckState state;
for (int i = 0; i < node.ParentNode.Nodes.Count; i++)
{
state = (CheckState)node.ParentNode.Nodes[i].CheckState;
if (!check.Equals(state))
{
b = !b;
break;
}
}
if (b)
{
node.ParentNode.CheckState = CheckState.Indeterminate;
}
else
{
node.ParentNode.CheckState = check;
}
SetCheckedParentNodes(node.ParentNode, check);
}
}
#endregion
2 获取所有选中状态的数据的主键集合 代码如下
private void barBtnSave_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
if (treeNavigation.Nodes.Count > 0)
{
foreach (TreeListNode root in treeNavigation.Nodes)
{
GetCheckedAuthorizatioId(root);
}
}
// AuthorizatioIdList 变量就是 选中状态的数据的主键集合
}
private List<string> AuthorizatioIdList = new List<string>();//所选择的权限的ID集合
/// <summary>
/// 获取选择状态的数据主键ID集合
/// </summary>
/// <param name="parentNode">父级节点</param>
private void GetCheckedAuthorizatioId(TreeListNode parentNode)
{
if (parentNode.Nodes.Count == 0)
{
return;//递归终止
}
foreach (TreeListNode node in parentNode.Nodes)
{
if (node.CheckState == CheckState.Checked)
{
DataRowView drv = treeNavigation.GetDataRecordByNode(node) as DataRowView;
if (drv != null)
{
string nav_id = drv["nav_id"].ToString();
AuthorizatioIdList.Add(nav_id);
}
}
GetCheckedAuthorizatioId(node);
}
}
3 一些常用的属性
//treeNavigation.OptionsView.AutoWidth = false; //禁用自动宽度
//treeNavigation.Columns["stateName"].Width = 180; //设置指定列宽
//treeNavigation.BorderStyle = DevExpress.XtraEditors.Controls.BorderStyles.NoBorder;
//默认显示下拉三角形样式,设置TreeList的两个皮肤属性即可变成连接虚线
treeNavigation.LookAndFeel.UseDefaultLookAndFeel = false;
treeNavigation.LookAndFeel.UseWindowsXPTheme = true;
treeNavigation.OptionsView.ShowHorzLines = false; //是否显示水平线。默认为True;
treeNavigation.OptionsView.ShowVertLines = true; //是否显示垂直线。默认为True;
treeNavigation.OptionsView.ShowIndicator = false; //是否显示Node的指示符面板,就是最左边有个三角箭头。默认为True;
treeNavigation.OptionsView.ShowButtons = true; //是否显示展开与收缩按钮。默认为True;
treeNavigation.OptionsView.ShowColumns = false; //是否显示列标题。默认为True;
treeNavigation.OptionsView.ShowIndentAsRowStyle = false; //是否用相应Node的Appearance设置来生成Tree的缩进(没看懂)。默认为False
treeNavigation.OptionsView.FocusRectStyle = DevExpress.XtraTreeList.DrawFocusRectStyle.None; //在获得焦点的Cell上,是否显示焦点框架。默认为True;
//treeNavigation.OptionsView.FocusRectStyle = DrawFocusRectStyle.CellFocus;
treeNavigation.OptionsView.ShowRoot = true; //是否在根Node间显示连接线。默认为True;
treeNavigation.OptionsView.ShowCheckBoxes = false; //显示勾选框。默认为False
treeNavigation.OptionsDragAndDrop.DragNodesMode = DragNodesMode.None;//拖拽节点
treeNavigation.OptionsBehavior.ReadOnly = true; //只读
treeNavigation.OptionsBehavior.Editable = false; //不可编辑
treeNavigation.OptionsBehavior.AllowIndeterminateCheckState = true; //设置节点是否有中间状态,即一部分子节点选中,一部分子节点没有选中
treeNavigation.OptionsSelection.InvertSelection = true; //选中风格是只应用于选中的Cell
treeNavigation.OptionsSelection.MultiSelect = false; //允许多选
treeNavigation.OptionsSelection.EnableAppearanceFocusedCell = true; //选中的Cell的Appearance设置是否可用
treeNavigation.ExpandAll(); //展开所有层级
// treeNavigation.ExpandToLevel(0); //展开的层级
注册这两个事件实现选择父节点自动选子节点,反之亦然
//treeNavigation.BeforeCheckNode += treeNavigation_BeforeCheckNode;
//treeNavigation.AfterCheckNode += treeNavigation_AfterCheckNode;
//treeNavigation.AfterFocusNode += treeNavigation_AfterFocusNode;
//treeNavigation.DoubleClick += treeNavigation_DoubleClick; //但要注意的是要在TreeList.OptionsBehavior.Editable = false的情况下,双击Node才能触发该事件
设置图片
treeNavigation.CustomDrawNodeImages += treeNavigation_CustomDrawNodeImages;
颜色设置
treeNavigation.Appearance.Row.BackColor = Color.Transparent;//节点默认背景色
treeNavigation.Appearance.FocusedRow.BackColor = Color.Orange; // 选中节点的背景色
treeNavigation.Appearance.HideSelectionRow.BackColor = Color.LightYellow;//选中节点失去焦点时的背景色
以上。