/
ComboBoxPopup.cs
386 lines (312 loc) · 14.8 KB
/
ComboBoxPopup.cs
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Media;
namespace MaterialDesignThemes.Wpf
{
public enum ComboBoxPopupPlacement
{
Undefined,
Down,
Up,
Classic
}
public class ComboBoxPopup : Popup
{
#region UpContentTemplate property
public static readonly DependencyProperty UpContentTemplateProperty
= DependencyProperty.Register(nameof(UpContentTemplate),
typeof(ControlTemplate),
typeof(ComboBoxPopup),
new UIPropertyMetadata(null, CreateTemplatePropertyChangedCallback(ComboBoxPopupPlacement.Classic)));
public ControlTemplate UpContentTemplate
{
get { return (ControlTemplate)GetValue(UpContentTemplateProperty); }
set { SetValue(UpContentTemplateProperty, value); }
}
#endregion
#region DownContentTemplate region
public static readonly DependencyProperty DownContentTemplateProperty
= DependencyProperty.Register(nameof(DownContentTemplate),
typeof(ControlTemplate),
typeof(ComboBoxPopup),
new UIPropertyMetadata(null, CreateTemplatePropertyChangedCallback(ComboBoxPopupPlacement.Down)));
public ControlTemplate DownContentTemplate
{
get { return (ControlTemplate)GetValue(DownContentTemplateProperty); }
set { SetValue(DownContentTemplateProperty, value); }
}
#endregion
#region ClassicContentTemplate property
public static readonly DependencyProperty ClassicContentTemplateProperty
= DependencyProperty.Register(nameof(ClassicContentTemplate),
typeof(ControlTemplate),
typeof(ComboBoxPopup),
new UIPropertyMetadata(null, CreateTemplatePropertyChangedCallback(ComboBoxPopupPlacement.Up)));
public ControlTemplate ClassicContentTemplate
{
get { return (ControlTemplate)GetValue(ClassicContentTemplateProperty); }
set { SetValue(ClassicContentTemplateProperty, value); }
}
#endregion
#region UpVerticalOffset property
public static readonly DependencyProperty UpVerticalOffsetProperty
= DependencyProperty.Register(nameof(UpVerticalOffset),
typeof(double),
typeof(ComboBoxPopup),
new PropertyMetadata(0.0));
public double UpVerticalOffset
{
get { return (double)GetValue(UpVerticalOffsetProperty); }
set { SetValue(UpVerticalOffsetProperty, value); }
}
#endregion
#region DownVerticalOffset property
public static readonly DependencyProperty DownVerticalOffsetProperty
= DependencyProperty.Register(nameof(DownVerticalOffset),
typeof(double),
typeof(ComboBoxPopup),
new PropertyMetadata(0.0));
public double DownVerticalOffset
{
get { return (double)GetValue(DownVerticalOffsetProperty); }
set { SetValue(DownVerticalOffsetProperty, value); }
}
#endregion
#region PopupPlacement property
public static readonly DependencyProperty PopupPlacementProperty
= DependencyProperty.Register(nameof(PopupPlacement),
typeof(ComboBoxPopupPlacement),
typeof(ComboBoxPopup),
new PropertyMetadata(ComboBoxPopupPlacement.Undefined, PopupPlacementPropertyChangedCallback));
public ComboBoxPopupPlacement PopupPlacement
{
get { return (ComboBoxPopupPlacement)GetValue(PopupPlacementProperty); }
set { SetValue(PopupPlacementProperty, value); }
}
#endregion
#region Background property
private static readonly DependencyPropertyKey BackgroundPropertyKey =
DependencyProperty.RegisterReadOnly(
"Background", typeof(Brush), typeof(ComboBoxPopup),
new PropertyMetadata(default(Brush)));
public static readonly DependencyProperty BackgroundProperty =
BackgroundPropertyKey.DependencyProperty;
public Brush Background
{
get { return (Brush) GetValue(BackgroundProperty); }
private set { SetValue(BackgroundPropertyKey, value); }
}
#endregion
#region DefaultVerticalOffset
public static readonly DependencyProperty DefaultVerticalOffsetProperty
= DependencyProperty.Register(nameof(DefaultVerticalOffset),
typeof(double),
typeof(ComboBoxPopup),
new PropertyMetadata(0.0));
public double DefaultVerticalOffset
{
get { return (double)GetValue(DefaultVerticalOffsetProperty); }
set { SetValue(DefaultVerticalOffsetProperty, value); }
}
#endregion
#region VisiblePlacementWidth
public double VisiblePlacementWidth
{
get { return (double)GetValue(VisiblePlacementWidthProperty); }
set { SetValue(VisiblePlacementWidthProperty, value); }
}
public static readonly DependencyProperty VisiblePlacementWidthProperty
= DependencyProperty.Register(nameof(VisiblePlacementWidth),
typeof(double),
typeof(ComboBoxPopup),
new PropertyMetadata(0.0));
#endregion
#region ClassicMode property
public static readonly DependencyProperty ClassicModeProperty
= DependencyProperty.Register(
nameof(ClassicMode),
typeof(bool),
typeof(ComboBoxPopup),
new FrameworkPropertyMetadata(true));
public bool ClassicMode
{
get { return (bool)GetValue(ClassicModeProperty); }
set { SetValue(ClassicModeProperty, value); }
}
#endregion
public ComboBoxPopup()
{
CustomPopupPlacementCallback = ComboBoxCustomPopupPlacementCallback;
DependencyPropertyDescriptor.FromProperty(ComboBoxPopup.ChildProperty, typeof(ComboBoxPopup))
.AddValueChanged(this,
delegate
{
if (PopupPlacement != ComboBoxPopupPlacement.Undefined)
{
UpdateChildTemplate(PopupPlacement);
}
});
}
private void SetupBackground(IEnumerable<DependencyObject> visualAncestry)
{
var background = visualAncestry
.Select(v => (v as Control)?.Background ?? (v as Panel)?.Background ?? (v as Border)?.Background)
.FirstOrDefault(v => v != null && !Equals(v, Brushes.Transparent) && v is SolidColorBrush);
if (background != null)
{
Background = background;
}
}
private void SetupVisiblePlacementWidth(IEnumerable<DependencyObject> visualAncestry)
{
var parent = visualAncestry.OfType<Panel>().ElementAt(1);
VisiblePlacementWidth = TreeHelper.GetVisibleWidth((FrameworkElement)PlacementTarget, parent);
}
private CustomPopupPlacement[] ComboBoxCustomPopupPlacementCallback(
Size popupSize, Size targetSize, Point offset)
{
var visualAncestry = PlacementTarget.GetVisualAncestry().ToList();
SetupBackground(visualAncestry);
SetupVisiblePlacementWidth(visualAncestry);
var data = GetPositioningData(visualAncestry, popupSize, targetSize, offset);
var preferUpIfSafe = data.LocationY + data.PopupSize.Height > data.ScreenHeight;
if (ClassicMode
|| data.LocationX + data.PopupSize.Width - data.RealOffsetX > data.ScreenWidth
|| data.LocationX - data.RealOffsetX < 0
|| (!preferUpIfSafe && (data.LocationY - Math.Abs(data.NewDownY) < 0)))
{
SetCurrentValue(PopupPlacementProperty, ComboBoxPopupPlacement.Classic);
return new[] { GetClassicPopupPlacement(this, data) };
}
if (preferUpIfSafe)
{
SetCurrentValue(PopupPlacementProperty, ComboBoxPopupPlacement.Up);
return new[] { GetUpPopupPlacement(data) };
}
SetCurrentValue(PopupPlacementProperty, ComboBoxPopupPlacement.Down);
return new[] { GetDownPopupPlacement(data) };
}
private void SetChildTemplateIfNeed(ControlTemplate template)
{
var contentControl = Child as ContentControl;
if (contentControl == null) return;
//throw new InvalidOperationException($"The type of {nameof(Child)} must be {nameof(ContentControl)}");
if (!ReferenceEquals(contentControl.Template, template))
{
contentControl.Template = template;
}
}
private PositioningData GetPositioningData(IEnumerable<DependencyObject> visualAncestry, Size popupSize, Size targetSize, Point offset)
{
var locationFromScreen = PlacementTarget.PointToScreen(new Point(0, 0));
var mainVisual = visualAncestry.OfType<Visual>().LastOrDefault();
if (mainVisual == null) throw new ArgumentException($"{nameof(visualAncestry)} must contains unless one {nameof(Visual)} control inside.");
var screenWidth = (int)DpiHelper.TransformToDeviceX(mainVisual, SystemParameters.PrimaryScreenWidth);
var screenHeight = (int)DpiHelper.TransformToDeviceY(mainVisual, SystemParameters.PrimaryScreenHeight);
var locationX = (int)locationFromScreen.X % screenWidth;
var locationY = (int)locationFromScreen.Y % screenHeight;
var upVerticalOffsetIndepent = DpiHelper.TransformToDeviceY(mainVisual, UpVerticalOffset);
var newUpY = upVerticalOffsetIndepent - popupSize.Height + targetSize.Height;
var newDownY = DpiHelper.TransformToDeviceY(mainVisual, DownVerticalOffset);
double offsetX;
const int rtlHorizontalOffset = 20;
if (FlowDirection == FlowDirection.LeftToRight)
offsetX = DpiHelper.TransformToDeviceX(mainVisual, offset.X);
else
offsetX = DpiHelper.TransformToDeviceX(mainVisual,
offset.X - targetSize.Width - rtlHorizontalOffset);
return new PositioningData(
mainVisual, offsetX,
newUpY, newDownY,
popupSize, targetSize,
locationX, locationY,
screenHeight, screenWidth);
}
private static PropertyChangedCallback CreateTemplatePropertyChangedCallback(ComboBoxPopupPlacement popupPlacement)
{
return delegate (DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var popup = d as ComboBoxPopup;
if (popup == null) return;
var template = e.NewValue as ControlTemplate;
if (template == null) return;
if (popup.PopupPlacement == popupPlacement)
{
popup.SetChildTemplateIfNeed(template);
}
};
}
private void UpdateChildTemplate(ComboBoxPopupPlacement placement)
{
switch (placement)
{
case ComboBoxPopupPlacement.Classic:
SetChildTemplateIfNeed(ClassicContentTemplate);
break;
case ComboBoxPopupPlacement.Down:
SetChildTemplateIfNeed(DownContentTemplate);
break;
case ComboBoxPopupPlacement.Up:
SetChildTemplateIfNeed(UpContentTemplate);
break;
// default:
// throw new NotImplementedException($"Unexpected value {placement} of the {nameof(PopupPlacement)} property inside the {nameof(ComboBoxPopup)} control.");
}
}
private static void PopupPlacementPropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var popup = d as ComboBoxPopup;
if (popup == null) return;
if (!(e.NewValue is ComboBoxPopupPlacement)) return;
var placement = (ComboBoxPopupPlacement)e.NewValue;
popup.UpdateChildTemplate(placement);
}
private static CustomPopupPlacement GetClassicPopupPlacement(ComboBoxPopup popup, PositioningData data)
{
var defaultVerticalOffsetIndepent = DpiHelper.TransformToDeviceY(data.MainVisual, popup.DefaultVerticalOffset);
var newY = data.LocationY + data.PopupSize.Height > data.ScreenHeight
? -(defaultVerticalOffsetIndepent + data.PopupSize.Height)
: defaultVerticalOffsetIndepent + data.TargetSize.Height;
return new CustomPopupPlacement(new Point(data.OffsetX, newY), PopupPrimaryAxis.Horizontal);
}
private static CustomPopupPlacement GetDownPopupPlacement(PositioningData data)
{
return new CustomPopupPlacement(new Point(data.OffsetX, data.NewDownY), PopupPrimaryAxis.None);
}
private static CustomPopupPlacement GetUpPopupPlacement(PositioningData data)
{
return new CustomPopupPlacement(new Point(data.OffsetX, data.NewUpY), PopupPrimaryAxis.None);
}
private struct PositioningData
{
public Visual MainVisual { get; }
public double OffsetX { get; }
public double NewUpY { get; }
public double NewDownY { get; }
public double RealOffsetX => (PopupSize.Width - TargetSize.Width) / 2.0;
public Size PopupSize { get; }
public Size TargetSize { get; }
public double LocationX { get; }
public double LocationY { get; }
public double ScreenHeight { get; }
public double ScreenWidth { get; }
public PositioningData(Visual mainVisual, double offsetX, double newUpY, double newDownY, Size popupSize, Size targetSize, double locationX, double locationY, double screenHeight, double screenWidth)
{
MainVisual = mainVisual;
OffsetX = offsetX;
NewUpY = newUpY;
NewDownY = newDownY;
PopupSize = popupSize; TargetSize = targetSize;
LocationX = locationX; LocationY = locationY;
ScreenWidth = screenWidth; ScreenHeight = screenHeight;
}
}
}
}