public static void ResetAllWindowCommandsBrush(this MetroWindow window)
 {
     if (window.OverrideDefaultWindowCommandsBrush == null)
     {
         window.InvokeCommandButtons(x => x.ClearValue(Control.ForegroundProperty));
         window.WindowButtonCommands.ClearValue(Control.ForegroundProperty);
     }
     else
     {
         window.ChangeAllWindowCommandsBrush(window.OverrideDefaultWindowCommandsBrush);
     }
 }
        /// <summary>
        /// Adapts the WindowCommands to the theme of the first opened, topmost && && (top || right || left) flyout
        /// </summary>
        /// <param name="flyouts">All the flyouts! Or flyouts that fall into the category described in the summary.</param>
        /// <param name="resetBrush">An optional brush to reset the window commands brush to.</param>
        public static void HandleWindowCommandsForFlyouts(this MetroWindow window, IEnumerable<Flyout> flyouts, Brush resetBrush = null)
        {
            var allOpenFlyouts = flyouts.Where(x => x.IsOpen);
            
            var anyFlyoutOpen = allOpenFlyouts.Any(x => x.Position != Position.Bottom);
            if (!anyFlyoutOpen)
            {
                if (resetBrush == null)
                {
                    window.ResetAllWindowCommandsBrush();
                }
                else
                {
                    window.ChangeAllWindowCommandsBrush(resetBrush);
                }
            }

            var topFlyout = allOpenFlyouts
                .Where(x => x.Position == Position.Top)
                .OrderByDescending(Panel.GetZIndex)
                .FirstOrDefault();
            if (topFlyout != null)
            {
                window.UpdateWindowCommandsForFlyout(topFlyout);
            }
            else {
                var leftFlyout = allOpenFlyouts
                    .Where(x => x.Position == Position.Left)
                    .OrderByDescending(Panel.GetZIndex)
                    .FirstOrDefault();
                if (leftFlyout != null)
                {
                    window.UpdateWindowCommandsForFlyout(leftFlyout);
                }
                var rightFlyout = allOpenFlyouts
                    .Where(x => x.Position == Position.Right)
                    .OrderByDescending(Panel.GetZIndex)
                    .FirstOrDefault();
                if (rightFlyout != null)
                {
                    window.UpdateWindowCommandsForFlyout(rightFlyout);
                }
            }
        }
        public static void UpdateWindowCommandsForFlyout(this MetroWindow window, Flyout flyout)
        {
            Brush brush = null;

            if (flyout.Theme == FlyoutTheme.Accent)
            {
                brush = (Brush)flyout.FindResource("IdealForegroundColorBrush");
            }

            else if (flyout.ActualTheme == Theme.Light)
            {
                brush = (Brush)ThemeManager.LightResource["BlackBrush"];
            }

            else if (flyout.ActualTheme == Theme.Dark)
            {
                brush = (Brush)ThemeManager.DarkResource["BlackBrush"];
            }

             window.ChangeAllWindowCommandsBrush(brush);
        }
        /// <summary>
        /// Adapts the WindowCommands to the theme of the first opened, topmost && && (top || right) flyout
        /// </summary>
        /// <param name="flyouts">All the flyouts! Or flyouts that fall into the category described in the summary.</param>
        /// <param name="resetBrush">An optional brush to reset the window commands brush to.</param>
        public static void HandleWindowCommandsForFlyouts(this MetroWindow window, IEnumerable<Flyout> flyouts, Brush resetBrush = null)
        {
            var flyout = flyouts
                .Where(x => x.IsOpen && (x.Position == Position.Right || x.Position == Position.Top))
                .OrderByDescending(Panel.GetZIndex)
                .FirstOrDefault();

            if (flyout != null)
            {
                window.UpdateWindowCommandsForFlyout(flyout);
            }

            else if(resetBrush == null)
            {
                window.ResetAllWindowCommandsBrush();
            }

            else
            {
                window.ChangeAllWindowCommandsBrush(resetBrush);
            }
        }
        public static void UpdateWindowCommandsForFlyout(this MetroWindow window, Flyout flyout)
        {
            Brush brush = flyout.Foreground;

            window.ChangeAllWindowCommandsBrush(brush, flyout.Position);
        }