Skip to content

Commit 2a978a8

Browse files
alonfntKeboo
authored andcommitted
Add RoutedCommand to PopupBox - Issue:#1191 (#1195)
* Add RoutedCommand to PopupBox I think I have followed the requests, but I am happy to learn from my mistakes. * Fix CommandBindings * Fixes Adds the null check in _popup and updates the comment on the RoutedCommand.
1 parent 600316a commit 2a978a8

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

MainDemo.Wpf/Buttons.xaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,8 @@
155155
</ComboBox>
156156

157157
<StackPanel Grid.Row="5" Grid.ColumnSpan="2" Orientation="Horizontal" HorizontalAlignment="Right">
158-
<Button Content="_Save" />
159-
<Button Content="_Cancel">
158+
<Button Content="_Save" Command="{x:Static materialDesign:PopupBox.ClosePopupCommand}" />
159+
<Button Content="_Cancel" Command="{x:Static materialDesign:PopupBox.ClosePopupCommand}">
160160
<Button.Style>
161161
<Style TargetType="Button" BasedOn="{StaticResource MaterialDesignPopupBoxButton}">
162162
<Setter Property="Foreground" Value="Red" />

MaterialDesignThemes.Wpf/PopupBox.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,12 @@ public class PopupBox : ContentControl
103103
public const string PopupContentControlPartName = "PART_PopupContentControl";
104104
public const string PopupIsOpenStateName = "IsOpen";
105105
public const string PopupIsClosedStateName = "IsClosed";
106+
107+
/// <summary>
108+
/// Routed command to be used inside of a popup content to close it.
109+
/// </summary>
110+
public static RoutedCommand ClosePopupCommand = new RoutedCommand();
111+
106112
private PopupEx _popup;
107113
private ContentControl _popupContentControl;
108114
private ToggleButton _toggleButton;
@@ -394,7 +400,9 @@ public override void OnApplyTemplate()
394400
_popup = GetTemplateChild(PopupPartName) as PopupEx;
395401
_popupContentControl = GetTemplateChild(PopupContentControlPartName) as ContentControl;
396402
_toggleButton = GetTemplateChild(TogglePartName) as ToggleButton;
397-
403+
404+
_popup?.CommandBindings.Add(new CommandBinding(ClosePopupCommand, ClosePopupHandler));
405+
398406
if (_toggleButton != null)
399407
_toggleButton.PreviewMouseLeftButtonUp += ToggleButtonOnPreviewMouseLeftButtonUp;
400408

@@ -440,6 +448,11 @@ protected override void OnMouseEnter(MouseEventArgs e)
440448
base.OnMouseEnter(e);
441449
}
442450

451+
private void ClosePopupHandler(object sender, ExecutedRoutedEventArgs executedRoutedEventArgs)
452+
{
453+
IsPopupOpen = false;
454+
}
455+
443456
private void OnLayoutUpdated(object sender, EventArgs eventArgs)
444457
{
445458
if (_popupContentControl != null && _popup != null &&

0 commit comments

Comments
 (0)