/// <summary>
        /// Applies a theme to the element recursively.
        /// </summary>
        /// <param name="element">WPF element to apply theme to.</param>
        /// <param name="sender">Object that triggered the event</param>
        /// <param name="theme">Color theme to update with.</param>
        public static void ApplyTheme(this DependencyObject element, object sender, ColorTheme theme)
        {
            if (!element.Dispatcher.CheckAccess())
            {
                element.Dispatcher.Invoke(() => element.ApplyTheme(sender, theme));
                return;
            }
            if (element.IsUpdated(theme))
            {
                return;
            }
            RemoveConflictingTheme(element);

            if (element is TextBox)
            {
                TextBox t = element as TextBox;
                t.Foreground = new SolidColorBrush(theme.foreground);
                t.Background = Brushes.Transparent;
            }
            else if (element is TextBlock)
            {
                TextBlock t = element as TextBlock;
                t.Foreground = new SolidColorBrush(theme.foreground);
                t.Background = Brushes.Transparent;
            }
            else if (element is Label)
            {
                Label l = element as Label;
                l.Foreground  = new SolidColorBrush(theme.foreground);
                l.Background  = Brushes.Transparent;
                l.BorderBrush = Brushes.Transparent;
            }
            else if (element is ListBox)
            {
                ListBox l = element as ListBox;
                l.Background = new SolidColorBrush(theme.background);
                l.Foreground = new SolidColorBrush(theme.foreground);
                l.ResubscribeColorChangedHandler(sender, theme);
            }
            else if (element is Frame)
            {
                Frame f = element as Frame;
                f.Background = new SolidColorBrush(theme.background);
                f.Foreground = new SolidColorBrush(theme.foreground);
                f.ResubscribeColorChangedHandler(sender, theme);
            }
            else if (element is Window)
            {
                Window w = element as Window;
                w.Background = new SolidColorBrush(theme.background);
                w.Foreground = new SolidColorBrush(theme.foreground);
                w.UpdateColors(sender, theme);
            }
            else if (element is Panel)
            {
                Panel p = element as Panel;
                p.Background = new SolidColorBrush(theme.background);
                p.UpdateColors(sender, theme); // the window is being update but doesn't show correctly
            }
            else if (element is Page)
            {
                Page p = element as Page;
                p.Foreground = new SolidColorBrush(theme.foreground);
                p.Background = new SolidColorBrush(theme.background);
                p.UpdateColorsVisual(sender, theme);
            }
            else if (element is Control)
            {
                Control c = element as Control;
                c.Background  = new SolidColorBrush(theme.background);
                c.Foreground  = new SolidColorBrush(theme.foreground);
                c.BorderBrush = new SolidColorBrush(theme.foreground);
                c.UpdateColorsVisual(sender, theme);
            }
            else if (element is Border)
            {
                (element as Border).Background = new SolidColorBrush(theme.background);
            }
            else if (element is Path)
            {
                (element as Path).Stroke = new SolidColorBrush(theme.foreground);
            }
        }