Skip to content

Commit

Permalink
Add RoutedCommand to PopupBox - Issue:MaterialDesignInXAML#1191 (Mate…
Browse files Browse the repository at this point in the history
…rialDesignInXAML#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.
  • Loading branch information
alonfnt authored and Keboo committed Feb 24, 2019
1 parent 600316a commit 2a978a8
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
4 changes: 2 additions & 2 deletions MainDemo.Wpf/Buttons.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,8 @@
</ComboBox>

<StackPanel Grid.Row="5" Grid.ColumnSpan="2" Orientation="Horizontal" HorizontalAlignment="Right">
<Button Content="_Save" />
<Button Content="_Cancel">
<Button Content="_Save" Command="{x:Static materialDesign:PopupBox.ClosePopupCommand}" />
<Button Content="_Cancel" Command="{x:Static materialDesign:PopupBox.ClosePopupCommand}">
<Button.Style>
<Style TargetType="Button" BasedOn="{StaticResource MaterialDesignPopupBoxButton}">
<Setter Property="Foreground" Value="Red" />
Expand Down
15 changes: 14 additions & 1 deletion MaterialDesignThemes.Wpf/PopupBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,12 @@ public class PopupBox : ContentControl
public const string PopupContentControlPartName = "PART_PopupContentControl";
public const string PopupIsOpenStateName = "IsOpen";
public const string PopupIsClosedStateName = "IsClosed";

/// <summary>
/// Routed command to be used inside of a popup content to close it.
/// </summary>
public static RoutedCommand ClosePopupCommand = new RoutedCommand();

private PopupEx _popup;
private ContentControl _popupContentControl;
private ToggleButton _toggleButton;
Expand Down Expand Up @@ -394,7 +400,9 @@ public override void OnApplyTemplate()
_popup = GetTemplateChild(PopupPartName) as PopupEx;
_popupContentControl = GetTemplateChild(PopupContentControlPartName) as ContentControl;
_toggleButton = GetTemplateChild(TogglePartName) as ToggleButton;


_popup?.CommandBindings.Add(new CommandBinding(ClosePopupCommand, ClosePopupHandler));

if (_toggleButton != null)
_toggleButton.PreviewMouseLeftButtonUp += ToggleButtonOnPreviewMouseLeftButtonUp;

Expand Down Expand Up @@ -440,6 +448,11 @@ protected override void OnMouseEnter(MouseEventArgs e)
base.OnMouseEnter(e);
}

private void ClosePopupHandler(object sender, ExecutedRoutedEventArgs executedRoutedEventArgs)
{
IsPopupOpen = false;
}

private void OnLayoutUpdated(object sender, EventArgs eventArgs)
{
if (_popupContentControl != null && _popup != null &&
Expand Down

0 comments on commit 2a978a8

Please sign in to comment.