(十)c#Winform自定义控件-列表
2021-02-08 11:14
标签:hang ref play nbsp Dimension www end 通过 开源 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章。 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control 如果觉得写的还行,请点个 star 支持一下吧 欢迎前来交流探讨: 企鹅群568015492 列表控件将被拆分为2部分,一个元素,一个列表,列表需要支持主副标题,图标等 首先定义一个数据源类(其实更好的是应该接受object,然后通过绑定字段反射绑定数据,这样就不需要这个数据源类了,这里偷懒了) 我们创建元素控件,添加一个用户控件,命名UCListItemExt 需要提供一下属性 开放一个对外的设置数据源入口 再处理一下点击事件 至此功能完成,看下完整代码 设计样式如下 接着我们需要创建列表控件,添加用户控件,命名UCListExt 看下需要哪些属性 向外暴露一个设置数据源的函数前提
准备工作
开始
1 ///
1 [Description("标题"), Category("自定义")]
2 public string Title
3 {
4 get { return label1.Text; }
5 set { label1.Text = value; }
6 }
7 [Description("副标题"), Category("自定义")]
8 public string Title2
9 {
10 get { return label3.Text; }
11 set
12 {
13 label3.Text = value;
14 label3.Visible = !string.IsNullOrEmpty(value);
15 }
16 }
17
18 [Description("标题字体"), Category("自定义")]
19 public Font TitleFont
20 {
21 get { return label1.Font; }
22 set
23 {
24 label1.Font = value;
25 }
26 }
27
28 [Description("副标题字体"), Category("自定义")]
29 public Font Title2Font
30 {
31 get { return label3.Font; }
32 set
33 {
34 label3.Font = value;
35 }
36 }
37
38 [Description("背景色"), Category("自定义")]
39 public Color ItemBackColor
40 {
41 get { return this.BackColor; }
42 set
43 {
44 this.BackColor = value;
45 }
46 }
47
48 [Description("标题文本色"), Category("自定义")]
49 public Color ItemForeColor
50 {
51 get { return label1.ForeColor; }
52 set { label1.ForeColor = value; }
53 }
54
55 [Description("副标题文本色"), Category("自定义")]
56 public Color ItemForeColor2
57 {
58 get { return label3.ForeColor; }
59 set { label3.ForeColor = value; }
60 }
61
62 [Description("是否显示右侧更多箭头"), Category("自定义")]
63 public bool ShowMoreBtn
64 {
65 get { return label2.Visible; }
66 set { label2.Visible = value; ; }
67 }
68
69 [Description("项选中事件"), Category("自定义")]
70 public event EventHandler ItemClick;
71
72 ///
1 #region 设置数据
2 ///
1 private void item_MouseDown(object sender, MouseEventArgs e)
2 {
3 if (ItemClick != null)
4 {
5 ItemClick(this, e);
6 }
7 }
1 // 版权所有 黄正辉 交流群:568015492 QQ:623128629
2 // 文件名称:UCListItemExt.cs
3 // 创建日期:2019-08-15 16:01:34
4 // 功能描述:List
5 // 项目地址:https://gitee.com/kwwwvagaa/net_winform_custom_control
6 using System;
7 using System.Collections.Generic;
8 using System.ComponentModel;
9 using System.Drawing;
10 using System.Data;
11 using System.Linq;
12 using System.Text;
13 using System.Windows.Forms;
14
15 namespace HZH_Controls.Controls
16 {
17 [ToolboxItem(false)]
18 public partial class UCListItemExt : UserControl
19 {
20 [Description("标题"), Category("自定义")]
21 public string Title
22 {
23 get { return label1.Text; }
24 set { label1.Text = value; }
25 }
26 [Description("副标题"), Category("自定义")]
27 public string Title2
28 {
29 get { return label3.Text; }
30 set
31 {
32 label3.Text = value;
33 label3.Visible = !string.IsNullOrEmpty(value);
34 }
35 }
36
37 [Description("标题字体"), Category("自定义")]
38 public Font TitleFont
39 {
40 get { return label1.Font; }
41 set
42 {
43 label1.Font = value;
44 }
45 }
46
47 [Description("副标题字体"), Category("自定义")]
48 public Font Title2Font
49 {
50 get { return label3.Font; }
51 set
52 {
53 label3.Font = value;
54 }
55 }
56
57 [Description("背景色"), Category("自定义")]
58 public Color ItemBackColor
59 {
60 get { return this.BackColor; }
61 set
62 {
63 this.BackColor = value;
64 }
65 }
66
67 [Description("标题文本色"), Category("自定义")]
68 public Color ItemForeColor
69 {
70 get { return label1.ForeColor; }
71 set { label1.ForeColor = value; }
72 }
73
74 [Description("副标题文本色"), Category("自定义")]
75 public Color ItemForeColor2
76 {
77 get { return label3.ForeColor; }
78 set { label3.ForeColor = value; }
79 }
80
81 [Description("是否显示右侧更多箭头"), Category("自定义")]
82 public bool ShowMoreBtn
83 {
84 get { return label2.Visible; }
85 set { label2.Visible = value; ; }
86 }
87
88 [Description("项选中事件"), Category("自定义")]
89 public event EventHandler ItemClick;
90
91 ///
1 namespace HZH_Controls.Controls
2 {
3 partial class UCListItemExt
4 {
5 ///
1 private Font _titleFont = new Font("微软雅黑", 15F);
2 [Description("标题字体"), Category("自定义")]
3 public Font TitleFont
4 {
5 get { return _titleFont; }
6 set { _titleFont = value; }
7 }
8 private Font _title2Font = new Font("微软雅黑", 14F);
9 [Description("副标题字体"), Category("自定义")]
10 public Font Title2Font
11 {
12 get { return _title2Font; }
13 set { _title2Font = value; }
14 }
15
16 private Color _itemBackColor = Color.White;
17 [Description("标题背景色"), Category("自定义")]
18 public Color ItemBackColor
19 {
20 get { return _itemBackColor; }
21 set { _itemBackColor = value; }
22 }
23
24 private Color _itemSelectedBackColor = Color.FromArgb(73, 119, 232);
25
26 [Description("标题选中背景色"), Category("自定义")]
27 public Color ItemSelectedBackColor
28 {
29 get { return _itemSelectedBackColor; }
30 set { _itemSelectedBackColor = value; }
31 }
32
33 private Color _itemForeColor = Color.Black;
34
35 [Description("标题文本色"), Category("自定义")]
36 public Color ItemForeColor
37 {
38 get { return _itemForeColor; }
39 set { _itemForeColor = value; }
40 }
41 private Color _itemSelectedForeColor = Color.White;
42
43 [Description("标题选中文本色"), Category("自定义")]
44 public Color ItemSelectedForeColor
45 {
46 get { return _itemSelectedForeColor; }
47 set { _itemSelectedForeColor = value; }
48 }
49 private Color _itemForeColor2 = Color.FromArgb(73, 119, 232);
50
51 [Description("副标题文本色"), Category("自定义")]
52 public Color ItemForeColor2
53 {
54 get { return _itemForeColor2; }
55 set { _itemForeColor2 = value; }
56 }
57 private Color _itemSelectedForeColor2 = Color.White;
58
59 [Description("副标题选中文本色"), Category("自定义")]
60 public Color ItemSelectedForeColor2
61 {
62 get { return _itemSelectedForeColor2; }
63 set { _itemSelectedForeColor2 = value; }
64 }
65
66 private int _itemHeight = 60;
67
68 [Description("项高度"), Category("自定义")]
69 public int ItemHeight
70 {
71 get { return _itemHeight; }
72 set { _itemHeight = value; }
73 }
74
75 private bool _autoSelectFirst = true;
76 [Description("自动选中第一项"), Category("自定义")]
77 public bool AutoSelectFirst
78 {
79 get { return _autoSelectFirst; }
80 set { _autoSelectFirst = value; }
81 }
82 public delegate void ItemClickEvent(UCListItemExt item);
83 [Description("选中项事件"), Category("自定义")]
84 public event ItemClickEvent ItemClick;
85
86 private bool _selectedCanClick = false;
87 [Description("选中后是否可以再次触发点击事件"), Category("自定义")]
88 public bool SelectedCanClick
89 {
90 get { return _selectedCanClick; }
91 set { _selectedCanClick = value; }
92 }
93
94 ///
1 public void SetList(List
上一篇:使用httpClient远程调用