Skip to content

Commit b5be40c

Browse files
committed
feat: Click outside the Popup to close it
1 parent 1ec9f42 commit b5be40c

2 files changed

Lines changed: 25 additions & 7 deletions

File tree

src/WPFDevelopers.Shared/Controls/MultiSelectComboBox/MultiSelectComboBox.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ namespace WPFDevelopers.Controls
2626
[TemplatePart(Name = PART_DropDownPanel, Type = typeof(Panel))]
2727
[TemplatePart(Name = ListViewTemplateNameSearch, Type = typeof(DataGrid))]
2828
[TemplatePart(Name = DropDownScrollViewer, Type = typeof(ScrollViewer))]
29+
[TemplatePart(Name = PART_Border, Type = typeof(Border))]
2930

3031
public class MultiSelectComboBox : ListView
3132
{
@@ -36,6 +37,9 @@ public class MultiSelectComboBox : ListView
3637
private const string PART_DropDownPanel = "PART_DropDown";
3738
private const string ListViewTemplateNameSearch = "PART_SearchSelector";
3839
private const string DropDownScrollViewer = "DropDownScrollViewer";
40+
private const string PART_Border = "PART_Border";
41+
42+
public static readonly RoutedCommand ToggleDropDownCommand = new RoutedCommand();
3943

4044
public static readonly DependencyProperty IsDropDownOpenProperty =
4145
DependencyProperty.Register("IsDropDownOpen", typeof(bool), typeof(MultiSelectComboBox),
@@ -95,6 +99,7 @@ public static readonly DependencyProperty MaxDropDownHeightProperty
9599
private Panel _panelDropDown;
96100
private ScrollViewer _scrollViewer;
97101
private CheckBox _checkBox;
102+
private Border _border;
98103
private string _theLastText;
99104
private bool _isUpdating;
100105

@@ -353,8 +358,19 @@ public override void OnApplyTemplate()
353358
}
354359
SyncListViewViews();
355360
Loaded += OnMultiSelectComboBox_Loaded;
361+
_border = GetTemplateChild(PART_Border) as Border;
362+
if(_border != null)
363+
_border.MouseUp += OnBorder_MouseUp;
364+
365+
}
366+
367+
private void OnBorder_MouseUp(object sender, MouseButtonEventArgs e)
368+
{
369+
IsDropDownOpen = !IsDropDownOpen;
370+
e.Handled = true;
356371
}
357372

373+
358374
private void OnMultiSelectComboBox_Loaded(object sender, RoutedEventArgs e)
359375
{
360376
UpdateText();

src/WPFDevelopers.Shared/Themes/MultiSelectComboBox.xaml

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@
117117
</Storyboard>
118118
</ControlTemplate.Resources>
119119
<Border
120-
Name="PART_Border"
120+
x:Name="PART_Border"
121121
Width="{TemplateBinding Width}"
122122
Height="{TemplateBinding Height}"
123123
HorizontalAlignment="{TemplateBinding HorizontalAlignment}"
@@ -127,19 +127,21 @@
127127
BorderThickness="{TemplateBinding BorderThickness}"
128128
CornerRadius="{Binding Path=(helpers:ElementHelper.CornerRadius), RelativeSource={RelativeSource TemplatedParent}}"
129129
SnapsToDevicePixels="True">
130-
<Grid Grid.IsSharedSizeScope="True">
130+
<Grid Background="Transparent" Grid.IsSharedSizeScope="True">
131131
<Grid.ColumnDefinitions>
132132
<ColumnDefinition Width="*" />
133133
<ColumnDefinition Width="Auto" SharedSizeGroup="ComboBoxButton" />
134134
</Grid.ColumnDefinitions>
135135
<ScrollViewer
136+
x:Name="PART_TagScrollViewer"
136137
Margin="{TemplateBinding Padding}"
137138
HorizontalScrollBarVisibility="Disabled"
138-
VerticalScrollBarVisibility="Auto">
139+
VerticalScrollBarVisibility="Auto"
140+
Visibility="Collapsed">
139141
<controls:SimpleWrapPanel
140142
x:Name="PART_SimpleWrapPanel"
141-
Spacing="2"
142-
Visibility="Collapsed" />
143+
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
144+
Spacing="2" />
143145
</ScrollViewer>
144146
<TextBox
145147
Name="PART_EditableTextBox"
@@ -184,7 +186,7 @@
184186
IsOpen="{Binding IsDropDownOpen, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}"
185187
Placement="Bottom"
186188
PlacementTarget="{Binding RelativeSource={RelativeSource TemplatedParent}}"
187-
StaysOpen="False">
189+
StaysOpen="True">
188190
<controls:SmallPanel
189191
x:Name="PART_DropDown"
190192
MinWidth="{TemplateBinding FrameworkElement.ActualWidth}"
@@ -286,7 +288,7 @@
286288
</Border>
287289
<ControlTemplate.Triggers>
288290
<Trigger Property="ShowType" Value="Tag">
289-
<Setter TargetName="PART_SimpleWrapPanel" Property="Visibility" Value="Visible" />
291+
<Setter TargetName="PART_TagScrollViewer" Property="Visibility" Value="Visible" />
290292
<Setter TargetName="PART_EditableTextBox" Property="Visibility" Value="Collapsed" />
291293
<Setter TargetName="PART_SimpleWrapPanel" Property="MinHeight" Value="21.6" />
292294
</Trigger>

0 commit comments

Comments
 (0)