duheng
2024-10-22 a95dc42495472cf931db97f8ccbe9d075708aa0b
WinFrmUI/HStation.WinFrmUI.Basic/01-SysCatalogManage/SysCatalogManageMainPanel.cs
@@ -1,4 +1,11 @@
using DevExpress.XtraLayout.Customization.Behaviours;
using DevExpress.Utils.DragDrop;
using DevExpress.XtraEditors;
using DevExpress.XtraTreeList;
using DevExpress.XtraTreeList.Handler;
using DevExpress.XtraTreeList.Nodes;
using SqlSugar;
using System.ComponentModel;
using System.Reflection;
namespace HStation.WinFrmUI.Basic
{
@@ -18,6 +25,8 @@
        private Yw.BLL.SysCatalog _bll;
        private List<SysCatalogViewModel> _IndexList;
        public override void InitialDataSource()
        {
            this.typeTreeListCtrl2.SetBindingData();
@@ -34,8 +43,8 @@
            {
                _allBindingList.Add(new SysCatalogViewModel(item));
            }
            _allBindingList = _allBindingList.OrderBy(x => x.SortCode).ToList();
            this.catalogViewModelBindingSource.DataSource = _allBindingList;
            _IndexList = _allBindingList.Select(x => new SysCatalogViewModel(x)).ToList();
            this.catalogViewModelBindingSource.ResetBindings(false);
            this.treeList1.ExpandAll();
        }
@@ -148,7 +157,7 @@
                return;
            }
            var dlg = new UpdateCatalogParentDlg();
            dlg.SetBindingData(currentVm.TypeID, currentVm.ID);
            dlg.SetBindingData(currentVm.TypeID, currentVm.ID, currentVm.ParentID);
            dlg.ReloadEvent += async (parentId) =>
            {
                var bol = await _bll.UpdateTreeSortCode(currentVm.ID, parentId, 1);
@@ -161,5 +170,98 @@
            };
            dlg.ShowDialog();
        }
        private TreeListNode currentDragNode = null;
        private void treeList1_DragDrop(object sender, DragEventArgs e)
        {
            currentDragNode = (TreeListNode)e.Data.GetData(typeof(TreeListNode));
            var currentvm = GetViewModel(currentDragNode);
            var dragInsertPosition = AjustDirection(sender, e);
            var targetNode = GetTargetNode(e);
            var targetVm = GetViewModel(targetNode);
            var currentIndex = _allBindingList.IndexOf(currentvm);  //操作目标索引
            var targetIndex = _allBindingList.IndexOf(targetVm);  //目标位置索引
            if (dragInsertPosition == DragInsertPosition.AsChild || targetVm == null || currentvm == null)
            {
                e.Effect = DragDropEffects.None;
                return;
            }
            if (dragInsertPosition == DragInsertPosition.After)
            {
                _IndexList.RemoveAt(currentIndex);
                _IndexList.Insert(targetIndex + 1, currentvm);
            }
            else if (dragInsertPosition == DragInsertPosition.Before)
            {
                _IndexList.RemoveAt(currentIndex);
                _IndexList.Insert(targetIndex - 1, currentvm);
            }
            if (currentvm.ParentID == targetVm.ParentID)
            {
                /*  var sortIndex = currentvm.SortCode;
                  currentvm.SortCode = targetVm.SortCode;
                  targetVm.SortCode = sortIndex;*/
                SetTreeListSorter(currentvm.ParentID);
                return;
            }
            e.Effect = DragDropEffects.None;
        }
        /// <summary>
        /// 获取拖动过程中的方向
        /// </summary>
        private DragInsertPosition AjustDirection(object sender, DragEventArgs e)
        {
            PropertyInfo pi = typeof(TreeList).GetProperty("Handler", BindingFlags.Instance | BindingFlags.NonPublic);
            TreeListHandler handler = (TreeListHandler)pi.GetValue(this.treeList1, null);
            return handler.StateData.DragInfo.DragInsertPosition;
        }
        private async void SetTreeListSorter(long parentId)
        {
            var sortList = new List<Yw.Vmo.Sorter>();
            var targetList = _IndexList.Where(x => x.ParentID == parentId).ToList();
            int i = 1;
            foreach (var item in targetList)
            {
                sortList.Add(new Yw.Vmo.Sorter { ID = item.ID, SortCode = i });
                i++;
            }
            if (!await _bll.UpdateSorter(sortList))
            {
                TipFormHelper.ShowError("修改排序失败!");
            }
            //  this.catalogViewModelBindingSource.ResetBindings(false);
        }
        private TreeListNode GetTargetNode(DragEventArgs e)
        {
            var point = new Point(e.X, e.Y);
            var clientPoint = this.treeList1.PointToClient(point);
            var targetNode = this.treeList1.GetNodeAt(clientPoint);
            return targetNode;
        }
        private SysCatalogViewModel GetViewModel(TreeListNode node)
        {
            if (node == null)
                return default;
            var vm = this.treeList1.GetDataRecordByNode(node) as SysCatalogViewModel;
            return vm;
        }
        private void barCheckSorter_CheckedChanged(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (barCheckSorter.Checked)
            {
                this.treeList1.OptionsDragAndDrop.DragNodesMode = DragNodesMode.Single;
            }
            else
            {
                this.treeList1.OptionsDragAndDrop.DragNodesMode = DragNodesMode.None;
            }
        }
    }
}