Shuxia Ning
2024-08-28 1a8a81785470302fc7fbd6914a9df5d1094dac2a
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
using DevExpress.XtraTreeList;
using DevExpress.XtraTreeList.Columns;
using DevExpress.XtraTreeList.Nodes;
using System.Collections;
 
namespace Verify
{
    public static class TreeListExtend
    {
        /// <summary>
        /// 根据clinet point 获取节点
        /// </summary>
        /// <param name="rhs"></param>
        /// <param name="cp"></param>
        /// <returns></returns>
        public static TreeListNode GetNodeByCP(this TreeList rhs, Point cp)
        {
            Point pt = rhs.PointToClient(cp);
            TreeListHitInfo ht = rhs.CalcHitInfo(pt);
            TreeListNode node = ht.Node;
            if (node is TreeListAutoFilterNode)
                return null;
            return node;
        }
 
        /// <summary>
        /// 初始化默认设置
        /// </summary>
        /// <param name="rhs"></param>
        /// <param name="height"></param>
        public static void InitialDefaultSettings(this TreeList rhs, int height = 30)
        {
            rhs.OptionsBehavior.Editable = false;
            rhs.OptionsBehavior.ReadOnly = true;
            rhs.OptionsSelection.EnableAppearanceFocusedCell = false;
            rhs.OptionsMenu.EnableNodeMenu = false;
            rhs.OptionsView.FocusRectStyle = DrawFocusRectStyle.None;
            rhs.OptionsSelection.SelectNodesOnRightClick = true;
            rhs.ViewStyle = TreeListViewStyle.TreeView;
            rhs.RowHeight = height;
 
            rhs.BorderStyle = DevExpress.XtraEditors.Controls.BorderStyles.NoBorder;
            rhs.OptionsFind.ShowCloseButton = false;
        }
 
        /// <summary>
        /// 初始化多选框设置
        /// </summary>
        /// <param name="rhs"></param>
        /// <param name="checkBoxFieldName"></param>
        /// <param name="height"></param>
        public static void InitialDefaultMultiSelectSettings(this TreeList rhs, string checkBoxFieldName = "Checked", int height = 30)
        {
            rhs.InitialDefaultSettings(height);
            rhs.CheckBoxFieldName = checkBoxFieldName;//多选框绑定列名
            rhs.OptionsBehavior.AllowRecursiveNodeChecking = true;//获取或设置父节点时是否自动检查/取消检查子节点   
            rhs.OptionsView.CheckBoxStyle = DevExpress.XtraTreeList.DefaultNodeCheckBoxStyle.Check;//设置勾选框样式
        }
 
        /// <summary>
        /// 初始化可编辑视图
        /// </summary> 
        public static void InitialBindingNormalEditView(this TreeList rhs, int height = 30)
        {
            rhs.OptionsSelection.EnableAppearanceFocusedCell = false;
            rhs.OptionsMenu.EnableNodeMenu = false;
            rhs.OptionsView.FocusRectStyle = DrawFocusRectStyle.None;
            rhs.OptionsSelection.SelectNodesOnRightClick = true;
            rhs.OptionsMenu.EnableColumnMenu = false;
            rhs.Appearance.HeaderPanel.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
            rhs.Appearance.Row.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Near;
            rhs.RowHeight = height;
        }
 
        /// <summary>
        /// 多列显示设置
        /// </summary>
        /// <param name="rhs"></param>
        /// <param name="height"></param>
        public static void InitialMultiColSettings(this TreeList rhs, int height = 30)
        {
            rhs.OptionsBehavior.Editable = false;
            rhs.OptionsBehavior.ReadOnly = true;
            rhs.OptionsSelection.EnableAppearanceFocusedCell = false;
            rhs.OptionsMenu.EnableNodeMenu = false;
            rhs.OptionsView.FocusRectStyle = DrawFocusRectStyle.None;
            rhs.OptionsSelection.SelectNodesOnRightClick = true;
            rhs.OptionsMenu.EnableColumnMenu = false;
            rhs.Appearance.HeaderPanel.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
            rhs.Appearance.Row.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
            rhs.RowHeight = height;
 
            rhs.BorderStyle = DevExpress.XtraEditors.Controls.BorderStyles.NoBorder;
            rhs.OptionsFind.ShowCloseButton = false;
        }
 
        /// <summary>
        /// 初始化多选框设置
        /// </summary>
        /// <param name="rhs"></param>
        /// <param name="checkBoxFieldName"></param>
        /// <param name="height"></param>
        public static void InitialMultiColMultiSelectSettings(this TreeList rhs, string checkBoxFieldName = "Checked", int height = 30)
        {
            rhs.InitialMultiColSettings(height);
            rhs.CheckBoxFieldName = checkBoxFieldName;//多选框绑定列名
            rhs.OptionsBehavior.AllowRecursiveNodeChecking = true;//获取或设置父节点时是否自动检查/取消检查子节点   
            rhs.OptionsView.CheckBoxStyle = DefaultNodeCheckBoxStyle.Check;//设置勾选框样式
        }
 
 
        //渐变色
        public static void RegistCustomDrawNodeCell(this TreeList rhs, Color? bgColor = null, Color? foreColor = null)
        {
            if (bgColor == null)
                bgColor = Color.FromArgb(0, 122, 204);
            if (foreColor == null)
                foreColor = Color.White;
            rhs.CustomDrawNodeCell += (sender, e) =>
            {
                if (e.Node == (sender as TreeList).FocusedNode)
                {
                    e.Appearance.BackColor = bgColor.Value;
                    e.Appearance.ForeColor = foreColor.Value;
                }
            };
        }
 
        /// <summary>
        /// 获取当前数据行
        /// </summary>
        public static T GetCurrentViewModel<T>(this TreeList tree, IEnumerable<T> source) where T : class
        {
            if (source == null)
                return default;
            if (source.Count() < 1)
            {
                return default;
            }
            var row = tree.GetFocusedRow() as T;
            if (row == null)
            {
                return null;
            }
            return row;
        }
 
 
        /*/// <summary>
        /// 导出Excel
        /// </summary> 
        public static void ExportExcel(this TreeList tree, DocumentPage page, string pageOperateInfo)
        {
            page.ShowCmdOperateInfo("导出列表信息到Excel");
            var path = ExcelSaveFilePathHelper.SaveFilePathName();
            page.ShowCmdOperateInfo(pageOperateInfo);
            if (string.IsNullOrEmpty(path)) return;
            tree.OptionsPrint.PrintAllNodes = true;//导出明细
            tree.OptionsPrint.PrintTree = true;//导出所有明细,false的话,只会导出展开的明细
            tree.ExportToXlsx(path, new DevExpress.XtraPrinting.XlsxExportOptionsEx() { ExportType = DevExpress.Export.ExportType.WYSIWYG, TextExportMode = DevExpress.XtraPrinting.TextExportMode.Text });
            DialogResult dr = XtraMessageBox.Show("导出成功!是否打开刚刚保存的Excel文件?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
            if (dr != DialogResult.OK)
                return;
            System.Diagnostics.Process.Start(path);
        }*/
 
        /// <summary>
        /// 设置拖拽状态下的普通视图
        /// </summary> 
        public static void SetDragViewModels<t>(this TreeList rhs) where t : class
        {
            rhs.DragDrop += (sender, e) =>
            {
                TreeListNode node = (TreeListNode)e.Data.GetData(typeof(TreeListNode));
                if (node == null) return;
                TreeList list = (TreeList)sender;
                if (list == node.TreeList) return;
                TreeListHitInfo info = list.CalcHitInfo(list.PointToClient(new Point(e.X, e.Y)));
                InsertBrush(list, node, info.Node == null ? -1 : info.Node.Id);
            };
        }
 
        private static void InsertBrush(TreeList list, TreeListNode node, int parent)
        {
            ArrayList data = new ArrayList();
            foreach (TreeListColumn column in node.TreeList.Columns)
            {
                data.Add(node[column]);
            }
            parent = list.AppendNode(data.ToArray(), parent).Id;
 
            if (node.HasChildren)
                foreach (TreeListNode n in node.Nodes)
                    InsertBrush(list, n, parent);
        }
 
 
 
        #region 刷新选中节点
        private static List<TreeListNode> _haveChildNodes = null;
        public static void RefreshCheckeState(this TreeList rhs)
        {
            if (rhs.OptionsView.CheckBoxStyle != DefaultNodeCheckBoxStyle.Check)
                return;
            _haveChildNodes = new List<TreeListNode>();
            foreach (TreeListNode node in rhs.Nodes)
            {
                GetHaveChildNode(node);
            }
            if (_haveChildNodes != null && _haveChildNodes.Count > 0)
            {
                _haveChildNodes.Reverse();
                foreach (var node in _haveChildNodes)
                {
                    if (node.Nodes != null && node.Nodes.Any())
                    {
                        foreach (var childNode in node.Nodes)
                        {
 
                        }
                        var checkCount = node.Nodes.Where(x => x.Checked).Count();
                        if (checkCount == 0)
                        {
                            node.CheckState = CheckState.Unchecked;
                        }
                        else if (checkCount == node.Nodes.Count)
                        {
                            node.CheckState = CheckState.Checked;
                        }
                        else
                        {
                            node.CheckState = CheckState.Indeterminate;
                        }
                    }
                }
            }
            _haveChildNodes = null;
        }
 
        private static void GetHaveChildNode(TreeListNode node)
        {
            if (node.Nodes.Count > 0)
            {
                _haveChildNodes.Add(node);
 
                foreach (TreeListNode childNode in node.Nodes)
                {
                    GetHaveChildNode(childNode);
                }
            }
        }
        #endregion
 
    }
}
 
 
 
/*/// <summary>
/// 设置拖拽状态下的普通视图
/// </summary> 
public static void SetDragViewModels<t>(this TreeList rhs) where t : class
{
    rhs.DragDrop += (sender, e) =>
    {
        TreeListNode node = (TreeListNode)e.Data.GetData(typeof(TreeListNode));
        if (node == null) return;
        TreeList list = (TreeList)sender;
        if (list == node.TreeList) return;
        TreeListHitInfo info = list.CalcHitInfo(list.PointToClient(new Point(e.X, e.Y)));
        InsertBrush(list, node, info.Node == null ? -1 : info.Node.Id);
    };
}
 
private static void InsertBrush(TreeList list, TreeListNode node, int parent)
{
    ArrayList data = new ArrayList();
    foreach (TreeListColumn column in node.TreeList.Columns)
    {
        data.Add(node[column]);
    }
    parent = list.AppendNode(data.ToArray(), parent).Id;
 
    if (node.HasChildren)
        foreach (TreeListNode n in node.Nodes)
            InsertBrush(list, n, parent);
}*/