diff --git a/MahApps.Metro/Behaviours/InternalCleanWindowGlueBehavior.cs b/MahApps.Metro/Behaviours/InternalCleanWindowGlueBehavior.cs index f8e123711a..123ce1a2c8 100644 --- a/MahApps.Metro/Behaviours/InternalCleanWindowGlueBehavior.cs +++ b/MahApps.Metro/Behaviours/InternalCleanWindowGlueBehavior.cs @@ -54,21 +54,8 @@ protected override void OnDetaching() void AssociatedMetroWindow_FlyoutsStatusChanged(object sender, RoutedEventArgs e) { - MetroWindow.FlyoutStatusChangedRoutedEventArgs args = e as MetroWindow.FlyoutStatusChangedRoutedEventArgs; - var flyout = args.ChangedFlyout; - - if (flyout.Position == Position.Right || flyout.Position == Position.Top) - { - if (flyout.IsOpen) - { - this.AssociatedMetroWindow.HandleFlyout(flyout, (Brush)this.AssociatedMetroWindow.FindResource("WhiteColorBrush")); - } - else - { - this.SetWindowCommandButtonsToBlackBrush(); - AssociatedMetroWindow.WindowButtonCommands.SetResourceReference(Control.ForegroundProperty, "BlackColorBrush"); - } - } + var flyouts = this.AssociatedMetroWindow.Flyouts.Items.Cast().ToList(); + this.AssociatedMetroWindow.HandleWindowCommandsForFlyouts(flyouts, (Brush) this.AssociatedMetroWindow.FindResource("BlackColorBrush")); } } } diff --git a/MahApps.Metro/Controls/Flyout.cs b/MahApps.Metro/Controls/Flyout.cs index b6f4e334eb..ee9975735d 100644 --- a/MahApps.Metro/Controls/Flyout.cs +++ b/MahApps.Metro/Controls/Flyout.cs @@ -126,13 +126,10 @@ public Flyout() this.ChangeFlyoutTheme(accent, windowTheme.Item1); } } - - ThemeManager.IsThemeChanged += this.ThemeManager_IsThemeChanged; }; - this.Unloaded += (sender, args) => ThemeManager.IsThemeChanged -= this.ThemeManager_IsThemeChanged; } - private void ChangeFlyoutTheme(Accent windowAccent, Theme windowTheme) + internal void ChangeFlyoutTheme(Accent windowAccent, Theme windowTheme) { // Beware: Über-dumb code ahead! switch (this.Theme) diff --git a/MahApps.Metro/Controls/MetroWindow.cs b/MahApps.Metro/Controls/MetroWindow.cs index 0b7f86067a..528be9f190 100644 --- a/MahApps.Metro/Controls/MetroWindow.cs +++ b/MahApps.Metro/Controls/MetroWindow.cs @@ -373,6 +373,27 @@ private void MetroWindow_Loaded(object sender, RoutedEventArgs e) { this.Flyouts = new FlyoutsControl(); } + + ThemeManager.IsThemeChanged += ThemeManagerOnIsThemeChanged; + this.Unloaded += (o, args) => ThemeManager.IsThemeChanged -= ThemeManagerOnIsThemeChanged; + } + + private void ThemeManagerOnIsThemeChanged(object sender, OnThemeChangedEventArgs e) + { + if (e.Accent != null) + { + var flyouts = this.Flyouts.Items.Cast().ToList(); + + if (!flyouts.Any()) + return; + + foreach (Flyout flyout in flyouts) + { + flyout.ChangeFlyoutTheme(e.Accent, e.Theme); + } + + this.HandleWindowCommandsForFlyouts(flyouts); + } } static MetroWindow() @@ -543,18 +564,7 @@ internal void HandleFlyoutStatusChange(Flyout flyout, IEnumerable visibl WindowCommandsPresenter.SetValue(Panel.ZIndexProperty, this.ShowWindowCommandsOnTop ? zIndex : (zIndex > 0 ? zIndex - 1 : 0)); WindowButtonCommands.SetValue(Panel.ZIndexProperty, zIndex); - // We need to adapt the window commands to the theme of the flyout - // This only needs to be done for the accent and light theme, - // the dark theme is the default one and will work out of the box - if (flyout.IsOpen) - { - this.HandleFlyout(flyout); - } - - else - { - this.ResetAllWindowCommandsBrush(); - } + this.HandleWindowCommandsForFlyouts(visibleFlyouts); } flyoutModal.Visibility = visibleFlyouts.Any(x => x.IsModal) ? Visibility.Visible : Visibility.Hidden; diff --git a/MahApps.Metro/Controls/MetroWindowHelpers.cs b/MahApps.Metro/Controls/MetroWindowHelpers.cs index d0440b342d..75f0785a97 100644 --- a/MahApps.Metro/Controls/MetroWindowHelpers.cs +++ b/MahApps.Metro/Controls/MetroWindowHelpers.cs @@ -13,11 +13,32 @@ namespace MahApps.Metro.Controls /// internal static class MetroWindowHelpers { - public static void ChangeAllWindowCommandsBrush(this MetroWindow window, Brush brush) + /// + /// Adapts the WindowCommands to the theme of the first opened, topmost && && (top || right) flyout + /// + /// All the flyouts! Or flyouts that fall into the category described in the summary. + /// An optional brush to reset the window commands brush to. + public static void HandleWindowCommandsForFlyouts(this MetroWindow window, IEnumerable flyouts, Brush resetBrush = null) { - window.ChangeWindowCommandButtonsBrush(brush); + var flyout = flyouts + .Where(x => x.IsOpen && (x.Position == Position.Right || x.Position == Position.Top)) + .OrderByDescending(Panel.GetZIndex) + .FirstOrDefault(); - window.WindowButtonCommands.SetValue(Control.ForegroundProperty, brush); + if (flyout != null) + { + window.UpdateWindowCommandsForFlyout(flyout); + } + + else if(resetBrush == null) + { + window.ResetAllWindowCommandsBrush(); + } + + else + { + window.ChangeAllWindowCommandsBrush(resetBrush); + } } public static void ResetAllWindowCommandsBrush(this MetroWindow window) @@ -27,25 +48,7 @@ public static void ResetAllWindowCommandsBrush(this MetroWindow window) window.WindowButtonCommands.ClearValue(Control.ForegroundProperty); } - public static void ChangeWindowCommandButtonsBrush(this MetroWindow window, string resourceName) - { - window.InvokeCommandButtons(x => x.SetResourceReference(Control.ForegroundProperty, resourceName)); - } - - public static void ChangeWindowCommandButtonsBrush(this MetroWindow window, Brush brush) - { - window.InvokeCommandButtons(x => x.SetValue(Control.ForegroundProperty, brush)); - } - - private static void InvokeCommandButtons(this MetroWindow window, Action