[C#/WINFORM/DEVEXPRESS] TreeList 클래스 : DragEnter/DragDrop/DragOver 이벤트를 사용해 드래그 & 드롭 사용하기
■ TreeList 클래스의 DragEnter/DragDrop/DragOver 이벤트를 사용해 드래그 & 드롭을 사용하는 방법을 보여준다. ▶ MainForm.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 |
using System; using System.ComponentModel; using System.Drawing; using System.Windows.Forms; using DevExpress.Utils; using DevExpress.XtraEditors; using DevExpress.XtraGrid; using DevExpress.XtraGrid.Views.Grid.ViewInfo; using DevExpress.XtraTreeList; using DevExpress.XtraTreeList.Nodes; namespace TestProject { /// <summary> /// 메인 폼 /// </summary> public partial class MainForm : XtraForm { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 그리드 히트 정보 /// </summary> private GridHitInfo gridHitInfo = null; /// <summary> /// 그리드 데이터 소스 /// </summary> private BindingList<Person> gridDataSource = new BindingList<Person>(); #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainForm() /// <summary> /// 생성자 /// </summary> public MainForm() { InitializeComponent(); this.gridDataSource.Add(new Person("John" , "Smith" , "USA" )); this.gridDataSource.Add(new Person("Michael", "Suyama" , "UK" )); this.gridDataSource.Add(new Person("Laura" , "Callahan", "UK" )); this.gridDataSource.Add(new Person("Gerard" , "Blain" , "France")); this.gridDataSource.Add(new Person("Sergio" , "Rubini" , "Italy" )); this.gridDataSource.Add(new Person("Andrew" , "Fuller" , "USA" )); this.gridControl.DataSource = this.gridDataSource; this.gridView.OptionsBehavior.EditorShowMode = EditorShowMode.Click; this.treeList.OptionsView.ShowIndentAsRowStyle = true; this.treeList.AllowDrop = true; this.treeList.KeyFieldName = "ID"; this.treeList.ParentFieldName = "ParentID"; this.treeList.DataSource = new BindingList<PersonExtended>(); this.gridControl.MouseDown += gridControl_MouseDown; this.gridControl.MouseMove += gridControl_MouseMove; this.treeList.DragEnter += treeList_DragEnter; this.treeList.DragDrop += treeList_DragDrop; this.treeList.DragOver += treeList_DragOver; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private #region 그리드 컨트롤 마우스 DOWN 처리하기 - gridControl_MouseDown(sender, e) /// <summary> /// 그리드 컨트롤 마우스 DOWN 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void gridControl_MouseDown(object sender, MouseEventArgs e) { this.gridHitInfo = this.gridView.CalcHitInfo(new Point(e.X, e.Y)); } #endregion #region 그리드 컨트롤 마우스 이동시 처리하기 - gridControl_MouseMove(sender, e) /// <summary> /// 그리드 컨트롤 마우스 이동시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void gridControl_MouseMove(object sender, MouseEventArgs e) { if(this.gridHitInfo == null) { return; } if(e.Button != MouseButtons.Left) { return; } Rectangle dragRectangle = new Rectangle ( new Point ( this.gridHitInfo.HitPoint.X - SystemInformation.DragSize.Width / 2, this.gridHitInfo.HitPoint.Y - SystemInformation.DragSize.Height / 2 ), SystemInformation.DragSize ); if(!(this.gridHitInfo.RowHandle == GridControl.InvalidRowHandle) && !dragRectangle.Contains(new Point(e.X, e.Y))) { object data = this.gridView.GetRow(gridHitInfo.RowHandle); this.gridControl.DoDragDrop(data, DragDropEffects.Copy); } } #endregion #region 트리 리스트 드래그 ENTER 처리하기 - treeList_DragEnter(sender, e) /// <summary> /// 트리 리스트 드래그 ENTER 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void treeList_DragEnter(object sender, DragEventArgs e) { e.Effect = DragDropEffects.Copy; } #endregion #region 트리 리스트 드래그 OVER 처리하기 - treeList_DragOver(sender, e) /// <summary> /// 트리 리스트 드래그 OVER 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void treeList_DragOver(object sender, DragEventArgs e) { e.Effect = DragDropEffects.Copy; } #endregion #region 트리 리스트 드래그 DROP 처리하기 - treeList_DragDrop(sender, e) /// <summary> /// 트리 리스트 드래그 DROP 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void treeList_DragDrop(object sender, DragEventArgs e) { TreeList treeList = sender as TreeList; DXDragEventArgs args = treeList.GetDXDragEventArgs(e); DragInsertPosition position = args.DragInsertPosition; Person persion = e.Data.GetData(typeof(TestProject.Person)) as Person; if(persion == null) { return; } int parentID = (int)treeList.RootValue; TreeListNode node = args.TargetNode; if(node == null) { treeList.AppendNode((new PersonExtended(persion, parentID)).ToArray(), null); } else { if(position == DragInsertPosition.AsChild) { parentID = Convert.ToInt32(node.GetValue("ID")); object[] persionValueArray = (new PersonExtended(persion, parentID)).ToArray(); treeList.AppendNode(persionValueArray, node); } if(position == DragInsertPosition.Before) { parentID = Convert.ToInt32(node.GetValue("ParentID")); object[] persionValueArray = (new PersonExtended(persion, parentID)).ToArray(); TreeListNode newNode = treeList.AppendNode(persionValueArray, node.ParentNode); int targetPosition; if(node.ParentNode == null) { targetPosition = treeList.Nodes.IndexOf(node); } else { targetPosition = node.ParentNode.Nodes.IndexOf(node); } treeList.SetNodeIndex(newNode, targetPosition); } node.Expanded = true; } } #endregion } } |
TestProject.zip