15 public partial class MultiSelectComboBox : UserControl
19 public MultiSelectComboBox()
21 InitializeComponent();
28 #region Dependency Properties
35 get => (IEnumerable)GetValue(ItemsSourceProperty);
36 set => SetValue(ItemsSourceProperty, value);
38 public static readonly DependencyProperty ItemsSourceProperty = DependencyProperty.Register(nameof(
ItemsSource), typeof(IEnumerable), typeof(
MultiSelectComboBox),
new PropertyMetadata(
null));
45 get => (IList)GetValue(SelectedItemsProperty);
46 set => SetValue(SelectedItemsProperty, value);
48 public static readonly DependencyProperty SelectedItemsProperty = DependencyProperty.Register(nameof(
SelectedItems), typeof(IList), typeof(
MultiSelectComboBox),
new FrameworkPropertyMetadata(
null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));
55 get => (DataTemplate)GetValue(ItemTemplateProperty);
56 set => SetValue(ItemTemplateProperty, value);
58 public static readonly DependencyProperty ItemTemplateProperty = DependencyProperty.Register(nameof(
ItemTemplate), typeof(DataTemplate), typeof(
MultiSelectComboBox),
new PropertyMetadata(
null));
65 get {
return (
bool)GetValue(IsPopupOpenProperty); }
66 set { SetValue(IsPopupOpenProperty, value); }
68 public static readonly DependencyProperty IsPopupOpenProperty = DependencyProperty.Register(nameof(
IsPopupOpen), typeof(
bool), typeof(
MultiSelectComboBox),
new PropertyMetadata(
false, OnIsPopupOpenChanged));
70 private static void OnIsPopupOpenChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
83 #region Popup Toggle Handling
99 Button removeButton = findParentOfType<Button>(e.OriginalSource as DependencyObject);
100 if (removeButton !=
null)
110 private void PART_ComboBoxContent_MouseEnter(
object sender, MouseEventArgs e)
112 PART_Popup.StaysOpen =
true;
116 private void PART_ComboBoxContent_MouseLeave(
object sender, MouseEventArgs e)
118 PART_Popup.StaysOpen =
false;
126 if (PART_Popup?.Child is DependencyObject popupChild)
128 foreach (var cb
in findVisualChildren<CheckBox>(popupChild))
130 MultiBindingExpression multi = BindingOperations.GetMultiBindingExpression(cb, CheckBox.IsCheckedProperty);
131 multi?.UpdateTarget();
140 #region CheckBox Handling
149 if (sender is CheckBox cb && cb.DataContext !=
null &&
SelectedItems !=
null)
165 if (sender is CheckBox cb && cb.DataContext !=
null &&
SelectedItems !=
null)
178 #region Remove Item Command
196 #region Helper Methods
206 DependencyObject current = child;
207 while (current !=
null)
213 current = VisualTreeHelper.GetParent(current);
228 for (
int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
230 DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
231 if (child !=
null && child is T t)
238 yield
return childOfChild;