diff --git a/MahApps.Metro/Controls/Flyout.cs b/MahApps.Metro/Controls/Flyout.cs index 2f28b85970..464d4afd35 100644 --- a/MahApps.Metro/Controls/Flyout.cs +++ b/MahApps.Metro/Controls/Flyout.cs @@ -23,12 +23,13 @@ public class Flyout : ContentControl public static readonly DependencyProperty HeaderProperty = DependencyProperty.Register("Header", typeof(string), typeof(Flyout), new PropertyMetadata(default(string))); public static readonly DependencyProperty PositionProperty = DependencyProperty.Register("Position", typeof(Position), typeof(Flyout), new PropertyMetadata(Position.Left, PositionChanged)); - public static readonly DependencyProperty IsPinnableProperty = DependencyProperty.Register("IsPinnable", typeof(bool), typeof(Flyout), new PropertyMetadata(default(bool))); + public static readonly DependencyProperty IsPinnedProperty = DependencyProperty.Register("IsPinned", typeof(bool), typeof(Flyout), new PropertyMetadata(true)); public static readonly DependencyProperty IsOpenProperty = DependencyProperty.Register("IsOpen", typeof(bool), typeof(Flyout), new FrameworkPropertyMetadata(default(bool), FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, IsOpenedChanged)); public static readonly DependencyProperty IsModalProperty = DependencyProperty.Register("IsModal", typeof(bool), typeof(Flyout)); public static readonly DependencyProperty HeaderTemplateProperty = DependencyProperty.Register("HeaderTemplate", typeof(DataTemplate), typeof(Flyout)); public static readonly DependencyProperty CloseCommandProperty = DependencyProperty.RegisterAttached("CloseCommand", typeof(ICommand), typeof(Flyout), new UIPropertyMetadata(null)); public static readonly DependencyProperty ThemeProperty = DependencyProperty.Register("Theme", typeof(FlyoutTheme), typeof(Flyout), new FrameworkPropertyMetadata(FlyoutTheme.Dark, ThemeChanged)); + public static readonly DependencyProperty ExternalCloseButtonProperty = DependencyProperty.Register("ExternalCloseButton", typeof(MouseButton), typeof(Flyout), new PropertyMetadata(MouseButton.Left)); /// /// Gets the actual theme (dark/light) of this flyout. @@ -64,12 +65,21 @@ public bool IsOpen } /// - /// Gets/sets whether this flyout is pinnable. + /// Gets/sets whether this flyout stays open when the user clicks outside of it. /// - public bool IsPinnable + public bool IsPinned { - get { return (bool)GetValue(IsPinnableProperty); } - set { SetValue(IsPinnableProperty, value); } + get { return (bool)GetValue(IsPinnedProperty); } + set { SetValue(IsPinnedProperty, value); } + } + + /// + /// Gets/sets the mouse button that closes the flyout on an external mouse click. + /// + public MouseButton ExternalCloseButton + { + get { return (MouseButton) GetValue(ExternalCloseButtonProperty); } + set { SetValue(ExternalCloseButtonProperty, value); } } /// diff --git a/MahApps.Metro/Controls/FlyoutsControl.cs b/MahApps.Metro/Controls/FlyoutsControl.cs index 9c5bf781dd..864c410614 100644 --- a/MahApps.Metro/Controls/FlyoutsControl.cs +++ b/MahApps.Metro/Controls/FlyoutsControl.cs @@ -6,6 +6,7 @@ using System.Linq; using System.Windows; using System.Windows.Controls; +using System.Windows.Input; namespace MahApps.Metro.Controls { @@ -16,6 +17,27 @@ namespace MahApps.Metro.Controls [StyleTypedProperty(Property = "ItemContainerStyle", StyleTargetType = typeof(Flyout))] public class FlyoutsControl : ItemsControl { + public static readonly DependencyProperty OverrideExternalCloseButtonProperty = DependencyProperty.Register("OverrideExternalCloseButton", typeof(MouseButton?), typeof(FlyoutsControl), new PropertyMetadata(null)); + public static readonly DependencyProperty OverrideIsPinnedProperty = DependencyProperty.Register("OverrideIsPinned", typeof(bool), typeof(FlyoutsControl), new PropertyMetadata(false)); + + /// + /// Gets/sets whether is ignored and all flyouts behave as if it was set to the value of this property. + /// + public MouseButton? OverrideExternalCloseButton + { + get { return (MouseButton?) GetValue(OverrideExternalCloseButtonProperty); } + set { SetValue(OverrideExternalCloseButtonProperty, value); } + } + + /// + /// Gets/sets whether is ignored and all flyouts behave as if it was set false. + /// + public bool OverrideIsPinned + { + get { return (bool) GetValue(OverrideIsPinnedProperty); } + set { SetValue(OverrideIsPinnedProperty, value); } + } + static FlyoutsControl() { DefaultStyleKeyProperty.OverrideMetadata( @@ -139,4 +161,4 @@ private void ReorderZIndices(Flyout lastChanged) } } } -} \ No newline at end of file +} diff --git a/MahApps.Metro/Controls/MetroWindow.cs b/MahApps.Metro/Controls/MetroWindow.cs index f30ea6d8f5..24b244e057 100644 --- a/MahApps.Metro/Controls/MetroWindow.cs +++ b/MahApps.Metro/Controls/MetroWindow.cs @@ -405,6 +405,30 @@ private void ThemeManagerOnIsThemeChanged(object sender, OnThemeChangedEventArgs this.HandleWindowCommandsForFlyouts(flyouts); } } + + private void FlyoutsPreviewKeyDown(object sender, MouseButtonEventArgs e) + { + if (Flyouts.OverrideExternalCloseButton == null) + { + foreach (Flyout flyout in Flyouts.Items) + { + if (flyout.ExternalCloseButton == e.ChangedButton && (flyout.IsPinned == false || Flyouts.OverrideIsPinned == true)) + { + flyout.IsOpen = false; + } + } + } + else if (Flyouts.OverrideExternalCloseButton == e.ChangedButton) + { + foreach (Flyout flyout in Flyouts.Items) + { + if (flyout.IsPinned == false || Flyouts.OverrideIsPinned == true) + { + flyout.IsOpen = false; + } + } + } + } static MetroWindow() { @@ -429,6 +453,8 @@ public override void OnApplyTemplate() overlayBox = GetTemplateChild(PART_OverlayBox) as Grid; metroDialogContainer = GetTemplateChild(PART_MetroDialogContainer) as Grid; flyoutModal = GetTemplateChild(PART_FlyoutModal) as Rectangle; + flyoutModal.PreviewMouseDown += FlyoutsPreviewKeyDown; + this.PreviewMouseDown += FlyoutsPreviewKeyDown; titleBar = GetTemplateChild(PART_TitleBar) as UIElement; @@ -593,4 +619,4 @@ internal FlyoutStatusChangedRoutedEventArgs(RoutedEvent rEvent, object source): public Flyout ChangedFlyout { get; internal set; } } } -} \ No newline at end of file +}