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
|
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Tango.Core.ExtensionMethods;
using Tango.SharedUI.Components;
using Tango.SharedUI.Controls;
namespace Tango.FSE.Common.Controls
{
public class SelectionComboBox : Control
{
private bool _preventAllSelected = false;
private bool _preventSelection = false;
private ICollectionView _filterView;
private TextBox _filterTextBox;
private Popup _popup;
private Button _toggle;
private bool _preventToggle;
public bool? AllSelected
{
get { return (bool?)GetValue(AllSelectedProperty); }
set { SetValue(AllSelectedProperty, value); }
}
public static readonly DependencyProperty AllSelectedProperty =
DependencyProperty.Register("AllSelected", typeof(bool?), typeof(SelectionComboBox), new PropertyMetadata(null, (d, e) => (d as SelectionComboBox).OnAllSelectedChanged()));
public ISelectedObjectCollection ItemsSource
{
get { return (ISelectedObjectCollection)GetValue(ItemsSourceProperty); }
set { SetValue(ItemsSourceProperty, value); }
}
public static readonly DependencyProperty ItemsSourceProperty =
DependencyProperty.Register("ItemsSource", typeof(ISelectedObjectCollection), typeof(SelectionComboBox), new PropertyMetadata(null, (d, e) => (d as SelectionComboBox).OnItemsSourceChanged()));
public String DisplayMemberPath
{
get { return (String)GetValue(DisplayMemberPathProperty); }
set { SetValue(DisplayMemberPathProperty, value); }
}
public static readonly DependencyProperty DisplayMemberPathProperty =
DependencyProperty.Register("DisplayMemberPath", typeof(String), typeof(SelectionComboBox), new PropertyMetadata(null));
public String Filter
{
get { return (String)GetValue(FilterProperty); }
set { SetValue(FilterProperty, value); }
}
public static readonly DependencyProperty FilterProperty =
DependencyProperty.Register("Filter", typeof(String), typeof(SelectionComboBox), new PropertyMetadata(null, (d, e) => (d as SelectionComboBox).OnFilterChanged()));
public bool IsOpened
{
get { return (bool)GetValue(IsOpenedProperty); }
set { SetValue(IsOpenedProperty, value); }
}
public static readonly DependencyProperty IsOpenedProperty =
DependencyProperty.Register("IsOpened", typeof(bool), typeof(SelectionComboBox), new PropertyMetadata(false));
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
if (!this.IsInDesignMode())
{
_filterTextBox = GetTemplateChild("filterTextBox") as TextBox;
_popup = GetTemplateChild("popup") as Popup;
_toggle = GetTemplateChild("toggle") as Button;
_toggle.Click += _toggle_Click;
_popup.Opened += _popup_Opened;
_popup.Closed += _popup_Closed;
}
}
private void _toggle_Click(object sender, RoutedEventArgs e)
{
if (!_preventToggle)
{
if (!_popup.IsOpen)
{
_popup.IsOpen = true;
}
else
{
_popup.IsOpen = false;
}
}
_preventToggle = false;
}
private async void _popup_Closed(object sender, EventArgs e)
{
_preventToggle = true;
await Task.Delay(100);
_preventToggle = false;
}
private void _popup_Opened(object sender, EventArgs e)
{
_filterTextBox.Focus();
}
private void OnItemsSourceChanged()
{
if (ItemsSource != null)
{
_filterView = CollectionViewSource.GetDefaultView(ItemsSource);
_filterView.Filter = ApplyFilter;
ItemsSource.SelectionChanged += ItemsSource_SelectionChanged;
}
}
private void ItemsSource_SelectionChanged(object sender, EventArgs e)
{
if (!_preventSelection)
{
if (ItemsSource.GetItems().All(x => x.IsSelected))
{
_preventAllSelected = true;
AllSelected = true;
}
else if (ItemsSource.GetItems().All(x => !x.IsSelected))
{
_preventAllSelected = true;
AllSelected = false;
}
else
{
_preventAllSelected = true;
AllSelected = null;
}
}
_preventAllSelected = false;
}
private void OnAllSelectedChanged()
{
_preventSelection = true;
if (!_preventAllSelected && AllSelected != null)
{
foreach (var item in ItemsSource.GetItems())
{
item.IsSelected = AllSelected.Value;
}
}
_preventSelection = false;
}
private void OnFilterChanged()
{
_filterView?.Refresh();
}
private bool ApplyFilter(object obj)
{
if (Filter.IsNotNullOrEmpty())
{
if (obj is SelectedObject selectedObject)
{
try
{
return selectedObject.Data.GetPropertyValueByPath(DisplayMemberPath).ToStringSafe().ToLower().Contains(Filter.ToLower());
}
catch
{
return true;
}
}
else
{
return false;
}
}
else
{
return true;
}
}
static SelectionComboBox()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(SelectionComboBox), new FrameworkPropertyMetadata(typeof(SelectionComboBox)));
}
}
}
|