duheng
2024-11-19 ca5f300a7cef85d22b5e0f9d59d117c49f9909b3
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
namespace Yw.WinFrmUI
{
    /// <summary>
    ///
    /// </summary>
    public static class TreeListExtensions
    {
        /// <summary>
        /// 根据 client point 获取节点
        /// </summary>
        public static TreeListNode GetNodeByCP(this TreeList rhs, Point cp)
        {
            Point pt = rhs.PointToClient(cp);
            DevExpress.XtraTreeList.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 = DevExpress.XtraTreeList.DrawFocusRectStyle.None;
            rhs.OptionsSelection.SelectNodesOnRightClick = true;
            rhs.ViewStyle = TreeListViewStyle.TreeView;
 
            rhs.RowHeight = height;
        }
 
        /// <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)
        {
            InitialDefaultSettings(rhs, 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 = DevExpress.XtraTreeList.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 = DevExpress.XtraTreeList.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;
        }
 
        /// <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)
        {
            InitialDefaultSettings(rhs, height);
            rhs.CheckBoxFieldName = checkBoxFieldName;//多选框绑定列名
            rhs.OptionsBehavior.AllowRecursiveNodeChecking = true;//获取或设置父节点时是否自动检查/取消检查子节点
            rhs.OptionsView.CheckBoxStyle = DevExpress.XtraTreeList.DefaultNodeCheckBoxStyle.Check;//设置勾选框样式
        }
 
        /// <summary>
        /// 渐变色
        /// </summary>
        /// <param name="rhs"></param>
        /// <param name="bgColor"></param>
        /// <param name="foreColor"></param>
        public static void RegistCustomDrawNodeCell(this TreeList rhs, System.Drawing.Color? bgColor = null, System.Drawing.Color? foreColor = null)
        {
            if (bgColor == null)
                bgColor = System.Drawing.Color.FromArgb(0, 122, 204);
            if (foreColor == null)
                foreColor = System.Drawing.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>
        /// 设置拖拽状态下的普通视图
        /// </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);
        }
    }
}