tangxu
2024-11-04 ebd031e3bed6c1cfddce8fc9b98f7f9e95fb9e32
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Windows.Forms;
using DPumpHydr.WinFrmUI.WenSkin.Controls.ListBoxControl;
 
namespace DPumpHydr.WinFrmUI.WenSkin.Controls
{
    [Designer(typeof(Design.Designer.WenListBoxControlDesigner))]
    public class WenListBoxControl : WenControl
    {
        public WenListBoxControl() : base()
        {
            flow = new FlowLayoutPanel()
            {
                Dock = DockStyle.Fill,
                AutoScroll = true,
                BorderStyle = BorderStyle.FixedSingle
            };
            this.Controls.Add(flow);
            Size = new Size(120, 120);
        }
 
        private FlowLayoutPanel flow;
        private WenListBoxControlItemCollection items;
 
        protected override void OnSizeChanged(EventArgs e)
        {
            base.OnSizeChanged(e);
            if (!AutoItemHight)
                foreach (Control control in flow.Controls)
                {
                    control.Width = this.Width - 25;
                }
        }
 
        #region 委托
        public delegate void ButtonClickEventHandler(object sender, ButtonClickEventArgs e);
 
        public class ButtonClickEventArgs : EventArgs
        {
            public object Value { get; set; }
            public Control ItemControl { get; set; }
            public string Text { get; set; }
            public string Name { get; set; }
            public WenListBoxControlItem Item { get; set; }
            public bool State { get; set; } = true;
        }
 
        public event ButtonClickEventHandler ButtonClick;
        public event ButtonClickEventHandler RemoveClick;
        public event ButtonClickEventHandler RemoveItemBeforeChanged;
        public event ButtonClickEventHandler ItemClick;
 
        protected virtual void OnRemoveClick(ButtonClickEventArgs e)
        {
            RemoveClick?.Invoke(this, e);
        }
        protected virtual void OnButtonClick(ButtonClickEventArgs e)
        {
            ButtonClick?.Invoke(this, e);
        }
        
        protected virtual void OnItemClick(ButtonClickEventArgs e)
        {
            ItemClick?.Invoke(this, e);
        }
        #endregion
 
        #region 公有属性
 
        [Category("Wen"), Description("选项值"), DefaultValue(null), DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        [Editor(typeof(Design.Editor.WenCollectionEditor), typeof(System.Drawing.Design.UITypeEditor))]
        public WenListBoxControlItemCollection Items => items ??= new WenListBoxControlItemCollection(this);
 
        [Category("Wen"), Description("设置模板 类请用typeof(Class)"), DefaultValue(typeof(WenListBoxControlItemTemplate))]
        public Type Template { get; set; } = typeof(WenListBoxControlItemTemplate);
 
        [Category("Wen"), Description("设置当行的高度"), DefaultValue(30)]
        public int ItemHight { get; set; } = 30;
 
        [Category("Wen"), Description("是否自动大小"), DefaultValue(false)]
        public bool AutoItemHight { get; set; } = false;
 
        [Category("Wen"), Description("是否显示删除按钮"), DefaultValue(true)]
        public bool RemoveButtonShow { get; set; } = true;
 
        [Category("WenImage"), Description("是否显示自定义按钮"), DefaultValue(true)]
        public bool ButtonShow { get; set; } = true;
        [Category("WenImage"), Description("自定义按钮图标"), DefaultValue(true)]
        public Image ButtonImage { get; set; }
        #endregion
 
        #region 绑定数据
        [Category("WenData"), Description("绑定数据列"), DefaultValue(null)]
        public string ColumnName { get; set; }
        private object dataSource { get; set; }
        [Browsable(false)]
        public object DataSource
        {
            get => dataSource;
            set
            {
                if (value != null)
                {
                    dataSource = value;
                    DataBind();
                }
            }
        }
        private void DataBind()
        {
            if (dataSource == null)
                return;
            this.Items.Clear();
 
            //分类数据绑定
            switch (dataSource)
            {
                case DataSet ds:
                    DataBindDataTable(ds.Tables[0]);
                    break;
                case DataTable dt:
                    DataBindDataTable(dt);
                    break;
                case System.Collections.IList ilist:
                    if (string.IsNullOrWhiteSpace(ColumnName))
                    {
                        foreach (var li in ilist)
                        {
                            List<System.Reflection.PropertyInfo> propertyInfos = new List<System.Reflection.PropertyInfo>(li.GetType().GetProperties());
                            string value = propertyInfos.Find(a => a.Name.ToUpper() == ColumnName.ToUpper())?.GetValue(li, null)?.ToString();
 
                            WenListBoxControlItem item = new WenListBoxControlItem()
                            {
                                Text = value,
                                Tag = li
                            };
                            Items.Add(item);
 
                        }
                    }
                    else
                    {
                        foreach (var li in ilist)
                        {
                            string value = li.GetType().GetProperties()[0].GetValue(li, null)?.ToString();
                            WenListBoxControlItem item = new WenListBoxControlItem()
                            {
                                Text = value,
                                Tag = li
                            };
                            Items.Add(item);
                        }
                    }
                    break;
                default:
                    break;
            }
 
            //绑定数据   
            void DataBindDataTable(DataTable dt)
            {
                if (string.IsNullOrWhiteSpace(ColumnName))
                {
                    foreach (DataRow dr in dt.Rows)
                    {
                        WenListBoxControlItem item = new WenListBoxControlItem()
                        {
                            Text = dr[0]?.ToString(),
                            Tag = dr
                        };
                        Items.Add(item);
                    }
                }
                else
                {
                    foreach (DataRow dr in dt.Rows)
                    {
                        WenListBoxControlItem item = new WenListBoxControlItem()
                        {
                            Text = dr[ColumnName]?.ToString(),
                            Tag = dr
                        };
                        Items.Add(dr[ColumnName]);
                    }
                }
            }
        }
 
 
        #endregion
 
        #region  子类
 
        public class WenListBoxControlItemCollection : WenCollection
        {
            private readonly WenListBoxControl owner;
            public WenListBoxControlItemCollection(WenListBoxControl owner)
            {
                this.owner = owner;
            }
 
            public WenListBoxControlItem this[int index]
            {
                get => Items[index] as WenListBoxControlItem;
                set
                {
                    if (value is WenListBoxControlItem item)
                        Items[index] = item;
                }
            }
            public WenListBoxControlItem this[string str]
            {
                get => Items.ToArray().ToList().Find(q =>
                (q as WenListBoxControlItem).Text.ToUpper() == str.ToUpper() ||
                (q as WenListBoxControlItem).Name.ToUpper() == str.ToUpper()) as WenListBoxControlItem;
            }
 
            public override int Add(object value)
            {
                if (value is WenListBoxControlItem controlItem)
                {
                    //自定义模板处理
                    if (controlItem.Template != null)
                    {
                        var type = System.Activator.CreateInstance(owner.Template);
                        if (type is Control control)
                        {
                            control.Text = controlItem.Text;
                            control.Name = controlItem.Name;
                            control.Tag = controlItem.Tag;
                            controlItem.Control = control;
                        }
                    }
                    else if (owner.Template != null)
                    {
                        controlItem.Template = owner.Template;
                        var type = System.Activator.CreateInstance(owner.Template);
                        if (type is Control control)
                        {
                            control.Text = controlItem.Text;
                            control.Name = controlItem.Name;
                            control.Tag = controlItem.Tag;
                            controlItem.Control = control;
                        }
                    }
 
                    //模板继承控件事件处理
                    if (controlItem.Control is WenListBoxControlItemTemplate template)
                    {
                        template.RemoveButtonShow = owner.RemoveButtonShow;
                        template.ButtonShow = owner.ButtonShow;
                        template.ButtonImage = owner.ButtonImage;
                        ButtonClickEventArgs ex = new ButtonClickEventArgs
                        {
                            Item = controlItem,
                            Text = controlItem.Text,
                            Name = controlItem.Name,
                            ItemControl = controlItem.Control,
                        };
                        //删除按钮点击事件移除内容
                        template.RemoveClick += (s, e) =>
                        {
                            ex.State = true;
                            this.owner.RemoveItemBeforeChanged(owner, ex);
                            if (ex.State)
                            {
                                this.Remove(controlItem);        
                            }
                            this.owner.OnRemoveClick(ex);
                        };
                        //自定义按钮点击事件
                        template.ButtonClick += (s, e) =>
                        {
                            owner.OnButtonClick(ex);
                        };
                        
                        //单击事件
                        template.Click += (s, e) =>
                        {
                            owner.OnItemClick(ex);
                        };
                    }
 
 
                    //添加单项
                    owner.flow.Controls.Add(controlItem.Control);
 
                    //是否自定义尺寸
                    if (!owner.AutoItemHight)
                    {
                        controlItem.Control.Width = owner.Width - 25;
                        controlItem.Control.Height = owner.ItemHight;
                    }
                }
                else
                {
                    return Add(value?.ToString());
                }
                return base.Add(value);
            }
 
            public int Add(string text)
            {
                WenListBoxControlItem controlItem = new WenListBoxControlItem(text);
                return this.Add(controlItem);
            }
            public override void Clear()
            {
                base.Clear();
                owner.flow.Controls.Clear();
            }
            public override void Remove(object value)
            {
                base.Remove(value);
                if (value is WenListBoxControlItem controlItem)
                {
                    owner.flow.Controls.Remove(controlItem.Control);
                }
            }
            public override void RemoveAt(int index)
            {
                base.RemoveAt(index);
                if (this[index] is WenListBoxControlItem controlItem)
                {
                    owner.flow.Controls.Remove(controlItem.Control);
                }
            }
        }
 
        public class WenListBoxControlItem
        {
            public WenListBoxControlItem()
            {
                value = "文本";
                Name = "Name";
                Tag = null;
            }
            public WenListBoxControlItem(string text) : this()
            {
                value = text;
            }
 
            public WenListBoxControlItem(object val)
            {
                value = val;
            }
 
            #region 私有属性
            private string name;
            private object tag;
            private object value;
            #endregion
 
            #region 公有属性
 
            /// <summary>
            /// 控件
            /// </summary>
            [Browsable(false)]
            public Control Control { get; set; }
            /// <summary>
            /// text内容
            /// </summary>
            [Category("Wen"), Description("内容"), DefaultValue(true)]
            public string Text
            {
                get => this.Value?.ToString();
                set => this.Value = value;
            }
            
            /// <summary>
            /// Value值
            /// </summary>
            [Category("Wen"), Description("内容"), DefaultValue(true)]
            public object Value
            {
                get => this.value;
                set
                {
                    this.value = value;
                    if (Control != null)
                    {
                        Control.Text = value?.ToString(); 
                        this.Control.Invalidate();
                    }
                }
            }
            /// <summary>
            /// 名称
            /// </summary>
            [Category("Wen"), Description("名称"), DefaultValue(true)]
            public string Name
            {
                get => name;
                set { name = value; if (Control != null) Control.Name = value; }
            }
 
            /// <summary>
            /// 绑定数据
            /// </summary>
            [Category("Wen"), Description("绑定数据"), DefaultValue(true)]
            public object Tag
            {
                get => tag;
                set { tag = value; if (Control != null) Control.Tag = value; }
            }
            /// <summary>
            /// 单项模板 请使用typeof(class)
            /// </summary>
            [Browsable(false)]
            [Category("Wen"), Description("单项模板"), DefaultValue(true)]
            public Type Template { get; set; }
 
            #endregion
 
            public override string ToString()
            {
                return Text;
            }
        }
 
        #endregion
    }
}