DevExpress之TreeList用法


在程序開發過程中,TreeList控件的使用頻率還是挺高的!本文對一些常用算法進行了一些總結:

1.TreeList數據綁定

    TreeList數據綁定采用數據源方式的綁定 

      TreeList.DataSource=List集合,DataTable,DataSet

 綁定之前設置2個基本的綁定屬性:KeyFieldName和ParentFieldName

    DataSource為對應 的DataTable,指定KeyFieldName為表主鍵字段,ParentFieldName為表指向主鍵的外鍵字段名。

2.TreeList節點

//鼠標單擊節點時,獲取節點信息
TreeListHitInfo hitinfo = emTreeList.CalcHitInfo(emTreeList.PointToClient(new Point(e.X, e.Y)));
TreeListNode node = hitinfo.Node;//當前點擊的節點

 

//獲取emTreeList控件中第一個節點中name的值

emTreeList.Nodes[0].GetValue("name")

 

//點擊節點前觸發的時間

privatevoid treeLstModuleAction_BeforeCheckNode(object sender, DevExpress.XtraTreeList.CheckNodeEventArgs e)
{
e.State
= (e.PrevState == CheckState.Checked ? CheckState.Unchecked : CheckState.Checked);
}

 

//點擊節點后觸發的時間

privatevoid treeLstModuleAction_AfterCheckNode(object sender, DevExpress.XtraTreeList.NodeEventArgs e)
{
SetCheckedChildNodes(e.Node, e.Node.CheckState);
SetCheckedParentNodes(e.Node, e.Node.CheckState);
}

 


/// 選擇子節點時觸發
privatevoid SetCheckedChildNodes(TreeListNode node, CheckState check)
{
for (int i =0; i < node.Nodes.Count; i++)
{
node.Nodes[i].CheckState
= check;
SetCheckedChildNodes(node.Nodes[i], check);
}
}

 


/// 選擇父節點時觸發
privatevoid SetCheckedParentNodes(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;
       }
   }
   node.ParentNode.CheckState
= b ? CheckState.Indeterminate : check;
   SetCheckedParentNodes(node.ParentNode, check);
 }
}

/// 判斷此節點下的所有孩子節點是否選中
private Boolean IsChildsChecked(TreeListNode node)
{
  for (int i =0; i < node.Nodes.Count; i++)
   {
     if (node.Nodes[i].CheckState == CheckState.Unchecked)
          return     false;
       if (node.Nodes[i].HasChildren)
        IsChildsChecked(node.Nodes[i]);
     }
          return  true;
}

 

////折疊當前節點及其所有子節點

 treeListPlate.CollapseAll();

//獲取焦點的節點

  TreeListNode node = treeListPlate.FocusedNode;

 


/// 選中后高亮顯示
private void treeListPlate_CustomDrawNodeCell(object sender, CustomDrawNodeCellEventArgs e)
{
        TreeList node = sender as TreeList;
         if (e.Node == node.FocusedNode)
        {
           e.Graphics.FillRectangle(SystemBrushes.Window, e.Bounds);
           Rectangle r = new Rectangle(e.EditViewInfo.ContentRect.Left,
           e.EditViewInfo.ContentRect.Top,
           Convert.ToInt32(e.Graphics.MeasureString(e.CellText, treeListPlate.Font).Width + 1),
           Convert.ToInt32(e.Graphics.MeasureString(e.CellText, treeListPlate.Font).Height));

           e.Graphics.FillRectangle(SystemBrushes.Highlight, r);
           e.Graphics.DrawString(e.CellText, treeListPlate.Font, SystemBrushes.HighlightText, r);

           e.Handled = true;
        }
}

 關於TreeList中節點的拖拽實現

要實現TreeList控件中節點的拖拽首先將控件中的AllowDrop屬性設置為True;

並實現 DragEnter(在用鼠標將某項拖動到該控件的工作區域是發生) 與  DragDrop(拖放操作完成時發生)這2個拖拽事件;

       /// <summary>
        /// 拖拽的節點
        /// </summary>
        private TreeListNode dragNode;
        /// <summary>
        /// 原始父節點
        /// </summary>
        private TreeListNode orgParentNode;
        /// <summary>
        /// 當前父節點
        /// </summary>
        private TreeListNode pNode = null;
        private int index;
 /// <summary>
        /// 拖拽完成時發生
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void emTreeList_DragDrop(object sender, DragEventArgs e)
        {
            //鼠標單擊節點時,獲取節點信息
            TreeListHitInfo hitinfo = emTreeList.CalcHitInfo(emTreeList.PointToClient(new Point(e.X, e.Y)));
            TreeListNode node = hitinfo.Node;//當前點擊的節點
            Console.WriteLine(node.GetValue("PUBLISHNAME"));
            Console.WriteLine("=="+index.ToString());
            
            try
            {
                if (null != node && null != dragNode)
                {
                    if (index == 0) //當前節點的子節點拖拽
                    {
                        pNode = node;
                    }
                    else //同級別
                    {
                        pNode = node.ParentNode;
                    }
                   
                    //此處根據需求自己寫邏輯
      

                    emTreeList.LockReloadNodes();
                }
            }
            catch (Exception)
            {
             MessageBox.Show("當前節點不被允許移動到當前位置!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }


        private void emTreeList_DragEnter(object sender, DragEventArgs e)
        {
          
            dragNode = emTreeList.FocusedNode;
            orgParentNode = dragNode.ParentNode;
        }

        private void emTreeList_CalcNodeDragImageIndex(object sender, DevExpress.XtraTreeList.CalcNodeDragImageIndexEventArgs e)
        {
            index = e.ImageIndex;
    
        }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM