private void OnAeroColorChanged(object sender, EventArgs e)
 {
     this.titleBar.Dispatcher.BeginInvoke((Action) delegate
     {
         this.titleBar.Background = AeroHelper.TryGetAeroColor();
         this.titleBar.Foreground = System.Windows.SystemColors.ActiveCaptionTextBrush;
     });
 }
        protected virtual void OnLoaded(object sender, RoutedEventArgs e)
        {
            // Inserting a custom titlebar before the window's content
            var grid = new Grid();

            grid.RowDefinitions.Add(new RowDefinition()
            {
                Height = new GridLength(CustomTitlebar.TitlebarHeightInPixels + 4, GridUnitType.Pixel)
            });

            grid.RowDefinitions.Add(new RowDefinition()
            {
                Height = new GridLength(1, GridUnitType.Star),
            });

            this.titleBar         = new CustomTitlebar();
            this.titleBar.Loaded += (ss, ee) =>
            {
                this.titleBar.Foreground = System.Windows.SystemColors.ActiveCaptionTextBrush;

                AeroHelper.AeroColorChanged += OnAeroColorChanged;
                if (AeroHelper.IsAeroEnabled)
                {
                    var aeroColor = AeroHelper.TryGetAeroColor();
                    if (aeroColor != null)
                    {
                        this.titleBar.Background = aeroColor;
                    }
                }
                else
                {
                    this.titleBar.Background = System.Windows.SystemColors.ActiveCaptionBrush;
                }
            };

            var content = this.Content as UIElement;

            this.Content = null;
            Grid.SetRow(this.titleBar, 0);
            Grid.SetRow(content, 1);
            grid.Children.Add(this.titleBar);
            grid.Children.Add(content);
            this.Content = grid;
            if (this.IsToolWindow)
            {
                this.MinWidth  = this.ActualWidth;
                this.MaxWidth  = this.ActualWidth;
                this.MinHeight = this.ActualHeight;
                this.MaxHeight = this.ActualHeight;
            }
        }