Shuxia Ning
2024-10-08 cf4967a0aebab18c5a37137f3e4c61b2d73a54bb
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
using IStation.Untity;
using System;
using System.ComponentModel;
using System.Linq;
 
namespace IStation.WinFrmUI.Basic
{
    public partial class SignalTypeTreeListLookUpEdit : DevExpress.XtraEditors.XtraUserControl
    {
        public SignalTypeTreeListLookUpEdit()
        {
            InitializeComponent();
            this.treeListLookUpEdit1.EditValueChanged += treeListLookUpEdit1_EditValueChanged;
            this.treeListLookUpEdit1TreeList.InitialDefaultSettings();
            this.treeListLookUpEdit1TreeList.SelectImageList = ImageLib.Lib;
            this.treeListLookUpEdit1TreeList.OptionsView.ShowIndicator = false;
            this.treeListLookUpEdit1.Properties.BestFitMode = DevExpress.XtraEditors.Controls.BestFitMode.BestFitResizePopup;
        }
 
 
        public class CurrentViewModel
        {
            public CurrentViewModel() { }
            public CurrentViewModel(Model.SignalTypeGroup rhs)
            {
                this.ID = rhs.ID;
                this.Name = rhs.Name;
                this.ParentID = TreeParentIdsHelper.GetLastParentID(rhs.ParentIds);
                this.Identifier = string.Empty;
                this.ImageIndex = ImageLib.Group;
                this.IsGroup = true;
                this.Model = rhs;
            }
 
            public CurrentViewModel(Model.SignalType rhs)
            {
                this.ID = rhs.ID;
                this.Name = rhs.Name;
                this.ParentID = rhs.GroupID;
                this.Identifier = rhs.Identifier;
                this.ImageIndex = ImageLib.SignalType;
                this.IsGroup = false;
                this.Model = rhs;
            }
 
            public long ID { get; set; }
            public string Name { get; set; }
            public string Identifier { get; set; }
            public long ParentID { get; set; }
            public int ImageIndex { get; set; }
            public bool IsGroup { get; set; }
            public object Model { get; set; }
        }
        /// <summary>
        /// 选择项
        /// </summary>
        public Model.SignalType Selected { get => _selected; }
        private Model.SignalType _selected { get; set; }
 
        /// <summary>
        /// 选择项变换
        /// </summary>
        public event Action<Model.SignalType> SelectedChangedEvent;
        private BindingList<CurrentViewModel> _allBindingList { get; set; }
 
 
        /// <summary>
        /// 绑定数据
        /// </summary> 
        public void SetBindingData(string identifier = null)
        {
            _allBindingList = new BindingList<CurrentViewModel>();
            var signalTypeGroups = new BLL.SignalTypeGroup().GetAll();
            if (signalTypeGroups != null && signalTypeGroups.Count > 0)
            {
                foreach (var item in signalTypeGroups)
                {
                    var vm = new CurrentViewModel(item);
                    _allBindingList.Add(vm);
                }
            }
 
            var signalTypes = new BLL.SignalType().GetAll();
            if (signalTypes != null && signalTypes.Count > 0)
            {
                foreach (var item in signalTypes)
                {
                    var vm = new CurrentViewModel(item);
                    _allBindingList.Add(vm);
                }
            }
 
            this.currentViewModelBindingSource.DataSource = _allBindingList;
 
 
            if (signalTypes != null && signalTypes.Any())
            {
                if (identifier == null)
                {
                    identifier = signalTypes.First().Identifier;
                }
                var vm = _allBindingList.Where(x => x.Identifier == identifier).FirstOrDefault();
                if (vm != null)
                {
                    this.treeListLookUpEdit1.EditValue = vm.ID;
                    SelectedChanged(vm);
                }
            }
 
            this.treeListLookUpEdit1TreeList.ExpandAll();
        }
 
 
        //选择对象变换
        private void SelectedChanged(CurrentViewModel vm)
        {
            _selected = vm?.Model as Model.SignalType;
            if (SelectedChangedEvent != null)
            {
                this.SelectedChangedEvent(_selected);
            }
        }
 
        //选中前
        private void treeListLookUpEdit1TreeList_BeforeFocusNode(object sender, DevExpress.XtraTreeList.BeforeFocusNodeEventArgs e)
        {
            var vm = this.treeListLookUpEdit1TreeList.GetDataRecordByNode(e.Node) as CurrentViewModel;
            if (vm != null)
            {
                e.CanFocus = !vm.IsGroup;
            }
        }
 
        //值变换
        private void treeListLookUpEdit1_EditValueChanged(object sender, EventArgs e)
        {
            var vm = this.treeListLookUpEdit1TreeList.GetCurrentViewModel(_allBindingList);
            if (vm != null)
            {
                SelectedChanged(vm);
            }
        }
 
 
    }
}