|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Diagnostics; |
| 4 | +using System.Linq; |
| 5 | +using System.Text; |
| 6 | +using System.Threading.Tasks; |
| 7 | +using System.Windows; |
| 8 | +using System.Windows.Controls; |
| 9 | +using System.Windows.Controls.Primitives; |
| 10 | + |
| 11 | +namespace MaterialDesignThemes.Wpf |
| 12 | +{ |
| 13 | + public class ComboBoxPopup : Popup |
| 14 | + { |
| 15 | + public static readonly DependencyProperty UpContentTemplateProperty |
| 16 | + = DependencyProperty.Register(nameof(UpContentTemplateProperty), |
| 17 | + typeof(ControlTemplate), |
| 18 | + typeof(ComboBoxPopup), |
| 19 | + new UIPropertyMetadata(null)); |
| 20 | + |
| 21 | + public ControlTemplate UpContentTemplate |
| 22 | + { |
| 23 | + get { return (ControlTemplate)GetValue(UpContentTemplateProperty); } |
| 24 | + set { SetValue(UpContentTemplateProperty, value); } |
| 25 | + } |
| 26 | + |
| 27 | + public static readonly DependencyProperty DownContentTemplateProperty |
| 28 | + = DependencyProperty.Register(nameof(DownContentTemplateProperty), |
| 29 | + typeof(ControlTemplate), |
| 30 | + typeof(ComboBoxPopup), |
| 31 | + new UIPropertyMetadata(null)); |
| 32 | + |
| 33 | + public ControlTemplate DownContentTemplate |
| 34 | + { |
| 35 | + get { return (ControlTemplate)GetValue(DownContentTemplateProperty); } |
| 36 | + set { SetValue(DownContentTemplateProperty, value); } |
| 37 | + } |
| 38 | + |
| 39 | + public static readonly DependencyProperty DefaultContentTemplateProperty |
| 40 | + = DependencyProperty.Register(nameof(DefaultContentTemplateProperty), |
| 41 | + typeof(ControlTemplate), |
| 42 | + typeof(ComboBoxPopup), |
| 43 | + new UIPropertyMetadata(null)); |
| 44 | + |
| 45 | + public ControlTemplate DefaultContentTemplate |
| 46 | + { |
| 47 | + get { return (ControlTemplate)GetValue(DefaultContentTemplateProperty); } |
| 48 | + set { SetValue(DefaultContentTemplateProperty, value); } |
| 49 | + } |
| 50 | + |
| 51 | + public static readonly DependencyProperty UpVerticalOffsetProperty |
| 52 | + = DependencyProperty.Register(nameof(UpVerticalOffsetProperty), |
| 53 | + typeof(double), |
| 54 | + typeof(ComboBoxPopup), |
| 55 | + new PropertyMetadata(0.0)); |
| 56 | + |
| 57 | + public double UpVerticalOffset |
| 58 | + { |
| 59 | + get { return (double)GetValue(UpVerticalOffsetProperty); } |
| 60 | + set { SetValue(UpVerticalOffsetProperty, value); } |
| 61 | + } |
| 62 | + |
| 63 | + public static readonly DependencyProperty DownVerticalOffsetProperty |
| 64 | + = DependencyProperty.Register(nameof(DownVerticalOffsetProperty), |
| 65 | + typeof(double), |
| 66 | + typeof(ComboBoxPopup), |
| 67 | + new PropertyMetadata(0.0)); |
| 68 | + |
| 69 | + public double DownVerticalOffset |
| 70 | + { |
| 71 | + get { return (double)GetValue(DownVerticalOffsetProperty); } |
| 72 | + set { SetValue(DownVerticalOffsetProperty, value); } |
| 73 | + } |
| 74 | + |
| 75 | + public static readonly DependencyProperty DefaultVerticalOffsetProperty |
| 76 | + = DependencyProperty.Register(nameof(DefaultVerticalOffsetProperty), |
| 77 | + typeof(double), |
| 78 | + typeof(ComboBoxPopup), |
| 79 | + new PropertyMetadata(0.0)); |
| 80 | + |
| 81 | + public double DefaultVerticalOffset |
| 82 | + { |
| 83 | + get { return (double)GetValue(DefaultVerticalOffsetProperty); } |
| 84 | + set { SetValue(DefaultVerticalOffsetProperty, value); } |
| 85 | + } |
| 86 | + |
| 87 | + public ComboBoxPopup() |
| 88 | + { |
| 89 | + this.CustomPopupPlacementCallback = ComboBoxCustomPopupPlacementCallback; |
| 90 | + } |
| 91 | + |
| 92 | + private void SetChildTemplateIfNeed(ControlTemplate template) |
| 93 | + { |
| 94 | + var contentControl = Child as ContentControl; |
| 95 | + if (contentControl == null) throw new InvalidOperationException("Child must be ContentControl"); |
| 96 | + |
| 97 | + if (!ReferenceEquals(contentControl.Template, template)) |
| 98 | + { |
| 99 | + contentControl.Template = template; |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + private CustomPopupPlacement[] ComboBoxCustomPopupPlacementCallback(Size popupSize, Size targetSize, |
| 104 | + Point offset) |
| 105 | + { |
| 106 | + var locationFromScreen = this.PlacementTarget.PointToScreen(new Point(0, 0)); |
| 107 | + |
| 108 | + int locationX = (int) locationFromScreen.X%(int) SystemParameters.PrimaryScreenWidth; |
| 109 | + int locationY = (int) locationFromScreen.Y%(int) SystemParameters.PrimaryScreenHeight; |
| 110 | + |
| 111 | + if (locationX + popupSize.Width > SystemParameters.PrimaryScreenWidth || locationX < 0) |
| 112 | + { |
| 113 | + SetChildTemplateIfNeed(DefaultContentTemplate); |
| 114 | + |
| 115 | + double newY = locationY + popupSize.Height > SystemParameters.PrimaryScreenHeight |
| 116 | + ? -(DefaultVerticalOffset + popupSize.Height) |
| 117 | + : DefaultVerticalOffset + targetSize.Height; |
| 118 | + |
| 119 | + return new[] { new CustomPopupPlacement(new Point(offset.X, newY), PopupPrimaryAxis.Horizontal) }; |
| 120 | + } |
| 121 | + if (locationY + popupSize.Height > SystemParameters.PrimaryScreenHeight) |
| 122 | + { |
| 123 | + SetChildTemplateIfNeed(UpContentTemplate); |
| 124 | + |
| 125 | + double newY = UpVerticalOffset - popupSize.Height + targetSize.Height; |
| 126 | + |
| 127 | + return new[] { new CustomPopupPlacement(new Point(offset.X, newY), PopupPrimaryAxis.None) }; |
| 128 | + } |
| 129 | + else |
| 130 | + { |
| 131 | + SetChildTemplateIfNeed(DownContentTemplate); |
| 132 | + |
| 133 | + double newY = DownVerticalOffset; |
| 134 | + |
| 135 | + return new[] { new CustomPopupPlacement(new Point(offset.X, newY), PopupPrimaryAxis.None) }; |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | +} |
0 commit comments