alielie
2026-06-15 f08871fe7d148d667f2f11f80f64de71a0d9504c
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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
 
namespace HMI.UI
{
    public class MultiComboBoxItem : ComboBoxItem, INotifyPropertyChanged
    {
        #region ...
        public event PropertyChangedEventHandler PropertyChanged;
 
        protected void RaisePropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion
 
        private MultiComboBox parent;
        public MultiComboBoxItem(MultiComboBox parent)
        {
            this.parent = parent;
            this.DataContextChanged += MultiComboBoxItem_DataContextChanged;
        }
 
        protected override void OnMouseMove(MouseEventArgs e)
        {
            this.PreviewMouseUp += MultiComboBoxItem_PreviewMouseUp;
            base.OnMouseMove(e);
        }
 
        protected override void OnMouseLeave(MouseEventArgs e)
        {
            this.PreviewMouseUp -= MultiComboBoxItem_PreviewMouseUp;
            base.OnMouseLeave(e);
        }
 
        protected override void OnLostFocus(RoutedEventArgs e)
        {
            this.PreviewMouseUp -= MultiComboBoxItem_PreviewMouseUp;
            base.OnLostFocus(e);
        }
 
        private void MultiComboBoxItem_PreviewMouseUp(object sender, MouseButtonEventArgs e)
        {
            if (e.OriginalSource is CheckBox)
            {
                // 从CheckBox点击传过来的事件要拉回焦点,不然会卡在Popup层当前的Item上
                this.Focus();
            }
            if (DataContext is MultiComboBoxData data)
            {
                data.IsChecked = !data.IsChecked;
                e.Handled = true;
            }
        }
 
        private void MultiComboBoxItem_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            if (e.OldValue != null && e.OldValue is MultiComboBoxData oldData)
            {
                oldData.IsCheckedChanged -= DataIsCheckedChangedChange;
            }
            if (DataContext != null && DataContext is MultiComboBoxData newData)
            {
                newData.IsCheckedChanged += DataIsCheckedChangedChange;
            }
        }
 
        private void DataIsCheckedChangedChange(object sender, bool isChecked)
        {
            parent.Item_IsCheckedChanged((MultiComboBoxData)sender, this, isChecked);
        }
    }
 
    public class MultiComboBoxDataConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is IEnumerable<object> items)
            {
                return items.Select(item => new MultiComboBoxData(item, "DisplayName", "DataValue")).ToList();
            }
            return null;
        }
 
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return value;
        }
    }
 
    public class MultiComboBoxData : INotifyPropertyChanged
    {
        #region ...
        public event PropertyChangedEventHandler PropertyChanged;
 
        protected void RaisePropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion
 
        public event EventHandler<bool> IsCheckedChanged;
 
        private bool isChecked;
 
        public bool IsChecked
        {
            get { return isChecked; }
            set
            {
                isChecked = value;
                RaisePropertyChanged("IsChecked");
                IsCheckedChanged?.Invoke(this, value);
            }
        }
        public string DisplayName
        {
            get => showName?.GetValue(Data).ToString();
        }
 
        public string DataValue
        {
            get => valueName?.GetValue(Data).ToString();
        }
 
        private PropertyInfo showName;
        private PropertyInfo valueName;
 
        public MultiComboBoxData(object data, string showName, string valueName)
        {
            this.Data = data;
            this.showName = data.GetType().GetProperty(showName);
            this.valueName = data.GetType().GetProperty(valueName);
        }
 
        public object Data { get; private set; }
    }
 
    /// <summary>
    /// MultiCombox.xaml 的交互逻辑
    /// </summary>
    public partial class MultiComboBox : ComboBox
    {
        public static readonly string[] splits = new string[] { ", " };
 
        protected override DependencyObject GetContainerForItemOverride()
        {
            var item = new MultiComboBoxItem(this);
            return item;
        }
 
        [Bindable(true)]
        [Category("Appearance")]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        [Localizability(LocalizationCategory.NeverLocalize)]
        public object SelectedValues
        {
            get
            {
                return GetValue(SelectedValuesProperty);
            }
            set
            {
                SetValue(SelectedValuesProperty, value);
            }
        }
 
        public static readonly DependencyProperty SelectedValuesProperty =
            DependencyProperty.RegisterAttached("SelectedValues", typeof(object), typeof(MultiComboBox),
                new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));
 
 
        public void Item_IsCheckedChanged(MultiComboBoxData data, MultiComboBoxItem item, bool isChecked)
        {
            var selectedValues = SelectedValues?.ToString() ?? "";
            var showNames = this.Text?.ToString() ?? "";
            List<string> values = string.IsNullOrEmpty(selectedValues) ? new List<string>() :
                selectedValues.Split(splits, StringSplitOptions.RemoveEmptyEntries).ToList();
            List<string> names = string.IsNullOrEmpty(showNames) ? new List<string>() :
                showNames.Split(splits, StringSplitOptions.RemoveEmptyEntries).ToList();
            if (isChecked)
            {
                values.Add(data.DataValue);
                names.Add(data.DisplayName);
            }
            else
            {
                values.Remove(data.DataValue);
                names.Remove(data.DisplayName);
            }
            Text = string.Join(", ", names);
            SelectedValues = string.Join(", ", values);
        }
    }
}