#region
using System;
using System.Data;
using System.Windows.Forms;
using CSFrameworkV5.Common;
using CSFrameworkV5.Core;
using DevExpress.XtraTreeList;
using DevExpress.XtraTreeList.Nodes;
#endregion
namespace CSFrameworkV5.Library.CommonClass
{
///
/// TreeList样式的表格
///
public class DevTreeListView : ISummaryView
{
private TreeList _TreeList;
///
/// DevTreeListView的构造器
///
///
public DevTreeListView(TreeList treeList)
{
_TreeList = treeList;
}
///
/// DevTreeListView的构造器
///
/// TreeList组件
/// 绑定弹出菜单,展开和收缩
public DevTreeListView(TreeList treeList, bool boundContextMenu)
{
_TreeList = treeList;
if (boundContextMenu) _TreeList.ContextMenu = CreateContextMenu();
}
///
/// 勾选或取消勾选当前结点的所有父结点
///
/// 当前结点
public static void CheckParent(TreeListNode currentNode, bool isCheck)
{
var tmp = currentNode;
currentNode = tmp.ParentNode;
while (currentNode != null)
{
currentNode.Checked = isCheck;
currentNode = currentNode.ParentNode;
}
//设置所有父节点的状态,当父结点的所有子结点没有勾选的情况下,父结点不勾选
currentNode = tmp.ParentNode;
while (currentNode != null)
{
if (HasChildChecked(currentNode) == false)
currentNode.Checked = false;
currentNode = currentNode.ParentNode;
}
}
private ContextMenu CreateContextMenu()
{
var menu = new ContextMenu();
menu.MenuItems.Add("展开树结点", OnExpandAll);
menu.MenuItems.Add("收缩全部", OnCollapseAll);
return menu;
}
///
/// 检查当前结点是否有子节点是勾选状态
///
///
///
public static bool HasChildChecked(TreeListNode node)
{
if (node.HasChildren)
foreach (TreeListNode N in node.Nodes)
if (N.Checked)
return true;
return false;
}
private void OnCollapseAll(object sender, EventArgs e)
{
_TreeList.CollapseAll();
}
private void OnExpandAll(object sender, EventArgs e)
{
_TreeList.ExpandAll();
}
///
/// 设置TreeList显示的图标
///
/// TreeList组件
/// 当前结点,从根结构递归时此值必须=null
/// 根结点图标(无子结点)
/// 有子结点的图标
public static void SetImageIndex(TreeList tl, TreeListNode node,
int nodeIndex, int parentIndex)
{
if (node == null)
{
foreach (TreeListNode N in tl.Nodes)
SetImageIndex(tl, N, nodeIndex, parentIndex);
}
else
{
if (node.HasChildren || node.ParentNode == null)
{
//node.SelectImageIndex = parentIndex;
node.StateImageIndex = parentIndex;
node.ImageIndex = parentIndex;
}
else
{
//node.SelectImageIndex = nodeIndex;
node.StateImageIndex = nodeIndex;
node.ImageIndex = nodeIndex;
}
foreach (TreeListNode N in node.Nodes)
SetImageIndex(tl, N, nodeIndex, parentIndex);
}
}
#region ISummaryView 成员
public void ExportData(string format, string file)
{
Msg.Warning("未实现ExportData方法!");
}
public int RowCount => (_TreeList.DataSource as DataTable).Rows.Count;
public int FocusedRowHandle
{
get => _TreeList.FocusedNode.Id;
set
{
var node = FindNode(value);
_TreeList.SetFocusedNode(node);
}
}
private TreeListNode FindNode(int rowIndex)
{
TreeListNode result = null;
foreach (TreeListNode n in _TreeList.Nodes)
{
if (n.Id == rowIndex) return n;
if (n.Nodes.Count > 0) FindChildNode(n, rowIndex, ref result);
}
return result;
}
private void FindChildNode(TreeListNode root, int rowIndex,
ref TreeListNode result)
{
foreach (TreeListNode n in root.Nodes)
{
if (n.Id == rowIndex) result = n;
if (n.Nodes.Count > 0) FindChildNode(n, rowIndex, ref result);
}
}
public object DataSource
{
get => _TreeList.DataSource;
set => _TreeList.DataSource = value;
}
public object View => _TreeList;
public DataRow GetDataRow(int rowHandle)
{
var source = _TreeList.DataSource as DataTable;
return source.Rows[rowHandle];
}
public void RefreshDataSource()
{
var o = _TreeList.DataSource;
_TreeList.DataSource = null;
_TreeList.DataSource = o;
_TreeList.RefreshDataSource();
_TreeList.Invalidate();
}
public void BindingDoubleClick(EventHandler eventHandler)
{
_TreeList.DoubleClick += eventHandler;
}
public void SetFocus()
{
_TreeList.Focus();
}
public void MoveFirst()
{
_TreeList.MoveFirst();
}
public void MovePrior()
{
_TreeList.MovePrev();
}
public void MoveNext()
{
_TreeList.MoveNext();
}
public void MoveLast()
{
_TreeList.MoveLast();
}
public bool IsValidRowHandle(int rowHandle)
{
return rowHandle >= 0 && rowHandle <=
(_TreeList.DataSource as DataTable).Rows.Count;
}
public void RefreshRow(int rowHandle)
{
_TreeList.Invalidate();
}
public void RemoveRow(int rowHandle)
{
var N = _TreeList.GetNodeByVisibleIndex(rowHandle);
_TreeList.DeleteNode(N);
}
#endregion
}
}