1
yhj
2024-07-24 5e5d945e91568b973faa27d8ab0bcef99fc4a6c5
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
#region
 
using System;
using System.ComponentModel;
using System.Data;
using System.Drawing.Design;
using System.Windows.Forms;
using DevExpress.XtraEditors;
 
#endregion
 
namespace CSFrameworkV5.Library.UserControls
{
    /// <summary>
    ///     编号-标题自定义控件.
    /// </summary>
    public partial class ucValueEdit : UserControl
    {
        private DataTable _ItemsData;
        private string[] _ItemsValue = { };
        private string _Seperator = "-";
 
        public ucValueEdit()
        {
            InitializeComponent();
 
            _ItemsData = new DataTable();
            _ItemsData.Columns.Add("Value", typeof(string));
            _ItemsData.Columns.Add("Name", typeof(string));
 
            _InnerLookUpEdit.Properties.DisplayMember = "Name";
            _InnerLookUpEdit.Properties.ValueMember = "Value";
            _InnerLookUpEdit.Properties.DataSource = _ItemsData;
        }
 
        [Category("Appearance")]
        [Description("嵌套的LookUpEdit控件")]
        public LookUpEdit InnerLookupEdit => _InnerLookUpEdit;
 
        [DesignerSerializationVisibility(
            DesignerSerializationVisibility.Content)]
        [Editor("System.Windows.Forms.Design.StringArrayEditor, System.Design",
            typeof(UITypeEditor))]
        [Category("Appearance")]
        [Description("自定义数据列表.如: A - 类型A ")]
        public string[] Items
        {
            get => _ItemsValue;
            set => SetItems(value);
        }
 
        [DefaultValue("-")]
        [Description("值与显示名称中间的分隔符.如:A - 类型A")]
        public string ItemValueSeperator
        {
            get => _Seperator;
            set => _Seperator = value;
        }
 
        private void RefreshDataSource()
        {
            _InnerLookUpEdit.Properties.DataSource = null;
            _InnerLookUpEdit.Properties.DataSource = _ItemsData;
        }
 
        /// <summary>
        ///     将String[]转换为DataTable
        /// </summary>
        /// <param name="values"></param>
        private void SetItems(string[] values)
        {
            _ItemsValue = values;
            _ItemsData.Rows.Clear();
 
            if (values.Length == 0) return;
 
            string[] P;
            foreach (var item in values)
            {
                var V = "";
                var N = "";
                if (item.Trim() != "")
                {
                    P = item.Split(char.Parse(_Seperator));
                    V = P.Length >= 1 ? P[0] : "";
                    N = P.Length == 2 ? P[1] : "";
                }
 
                _ItemsData.Rows.Add(V, N);
            }
 
            RefreshDataSource();
        }
 
        private void ucValueEdit_Load(object sender, EventArgs e)
        {
            SetItems(_ItemsValue);
        }
 
        private void ucValueEdit_SizeChanged(object sender, EventArgs e)
        {
            _InnerLookUpEdit.Width = Width;
            Height = _InnerLookUpEdit.Height;
        }
    }
}