Represents adorner for Backstage
Inheritance: System.Windows.Documents.Adorner
Exemplo n.º 1
0
        private void CreateAndAttachBackstageAdorner()
        {
            // It's possible that we created an adorner but it's parent AdornerLayer got destroyed.
            // If that's the case we have to destroy our adorner.
            // This fixes #228 Backstage disappears when changing DontUseDwm
            if (this.adorner?.Parent == null)
            {
                this.DestroyAdorner();
            }

            if (this.adorner != null)
            {
                return;
            }

            FrameworkElement elementToAdorn;

            if (DesignerProperties.GetIsInDesignMode(this))
            {
                // TODO: in design mode it is required to use design time adorner
                elementToAdorn = (FrameworkElement)VisualTreeHelper.GetParent(this);
            }
            else
            {
                elementToAdorn = UIHelper.GetParent <AdornerDecorator>(this)
                                 ?? UIHelper.GetParent <AdornerDecorator>(this.Parent);
            }

            if (elementToAdorn == null)
            {
                return;
            }

            AdornerDecorator currentAdornerDecorator;

            while ((currentAdornerDecorator = UIHelper.GetParent <AdornerDecorator>(elementToAdorn)) != null)
            {
                elementToAdorn = currentAdornerDecorator;
            }

            var layer = UIHelper.GetAdornerLayer(elementToAdorn);

            if (layer == null)
            {
                throw new Exception($"AdornerLayer could not be found for {this}.");
            }

            this.adorner = new BackstageAdorner(elementToAdorn, this)
            {
                DataContext = this.DataContext
            };

            layer.Add(this.adorner);

            layer.CommandBindings.Add(new CommandBinding(RibbonCommands.OpenBackstage, HandleOpenBackstageCommandExecuted, HandleOpenBackstageCommandCanExecute));
        }
Exemplo n.º 2
0
        private void CreateAndAttachBackstageAdorner()
        {
            // It's possible that we created an adorner but it's parent AdornerLayer got destroyed.
            // If that's the case we have to destroy our adorner.
            // This fixes #228 Backstage disappears when changing DontUseDwm
            if (this.adorner?.Parent == null)
            {
                this.DestroyAdorner();
            }

            if (this.adorner != null)
            {
                return;
            }

            FrameworkElement elementToAdorn = UIHelper.GetParent <AdornerDecorator>(this)
                                              ?? UIHelper.GetParent <AdornerDecorator>(this.Parent);

            if (elementToAdorn == null)
            {
                return;
            }

            if (this.UseHighestAvailableAdornerLayer)
            {
                AdornerDecorator currentAdornerDecorator;
                while ((currentAdornerDecorator = UIHelper.GetParent <AdornerDecorator>(elementToAdorn)) != null)
                {
                    elementToAdorn = currentAdornerDecorator;
                }
            }

            this.AdornerLayer = UIHelper.GetAdornerLayer(elementToAdorn);

            if (this.AdornerLayer == null)
            {
                throw new Exception($"AdornerLayer could not be found for {this}.");
            }

            this.adorner = new BackstageAdorner(elementToAdorn, this);

            BindingOperations.SetBinding(this.adorner, DataContextProperty, new Binding
            {
                Path   = new PropertyPath(DataContextProperty),
                Source = this
            });

            this.AdornerLayer.Add(this.adorner);

            this.AdornerLayer.CommandBindings.Add(new CommandBinding(RibbonCommands.OpenBackstage, HandleOpenBackstageCommandExecuted, HandleOpenBackstageCommandCanExecute));
        }
Exemplo n.º 3
0
        private void DestroyAdorner()
        {
            if (this.adorner == null)
            {
                return;
            }

            var layer = AdornerLayer.GetAdornerLayer(this);

            layer.Remove(this.adorner);

            this.adorner.Clear();
            this.adorner = null;
        }
Exemplo n.º 4
0
        private void DestroyAdorner()
        {
            this.AdornerLayer?.CommandBindings.Clear();
            this.AdornerLayer?.Remove(this.adorner);

            if (this.adorner != null)
            {
                BindingOperations.ClearAllBindings(this.adorner);
            }

            this.adorner?.Clear();
            this.adorner = null;

            this.AdornerLayer = null;
        }
Exemplo n.º 5
0
        private void DestroyAdorner()
        {
            if (this.adorner == null)
            {
                return;
            }

            var layer = AdornerLayer.GetAdornerLayer(this.adorner);

            layer?.CommandBindings.Clear();
            layer?.Remove(this.adorner);

            this.adorner.DataContext = null;

            this.adorner.Clear();
            this.adorner = null;
        }
Exemplo n.º 6
0
        /// <summary>
        /// When overridden in a derived class, is invoked whenever application code or internal processes call <see cref="M:System.Windows.FrameworkElement.ApplyTemplate"/>.
        /// </summary>
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            if (this.adorner == null)
            {
                return;
            }

            if (this.IsOpen)
            {
                this.Hide();
                this.IsOpen = false;
            }

            // Clear adorner
            this.adorner.Clear();
            this.adorner = null;
        }
Exemplo n.º 7
0
        private void CreateAndAttachBackstageAdorner()
        {
            if (this.adorner != null)
            {
                return;
            }

            if (DesignerProperties.GetIsInDesignMode(this))
            {
                // TODO: in design mode it is required to use design time adorner
                var topLevelElement = (FrameworkElement)VisualTreeHelper.GetParent(this);
                var topOffset       = this.TranslatePoint(new Point(0, this.ActualHeight), topLevelElement).Y;
                this.adorner = new BackstageAdorner(topLevelElement, this.Content, topOffset);
            }
            else
            {
                var mainWindow = Window.GetWindow(this);
                if (mainWindow == null)
                {
                    return;
                }

                var topLevelElement = (FrameworkElement)mainWindow.Content;
                if (topLevelElement == null)
                {
                    return;
                }

                var topOffset = this.TranslatePoint(new Point(0, this.ActualHeight), topLevelElement).Y;
                this.adorner = new BackstageAdorner(topLevelElement, this.Content, topOffset);
            }

            var layer = AdornerLayer.GetAdornerLayer(this);

            layer.Add(this.adorner);
        }
Exemplo n.º 8
0
        private void CreateAndAttachBackstageAdorner()
        {
            if (this.adorner != null)
            {
                return;
            }

            FrameworkElement topLevelElement = null;

            if (DesignerProperties.GetIsInDesignMode(this))
            {
                // TODO: in design mode it is required to use design time adorner
                topLevelElement = (FrameworkElement)VisualTreeHelper.GetParent(this);
            }
            else
            {
                var mainWindow = Window.GetWindow(this);
                if (mainWindow == null)
                {
                    return;
                }

                var content = mainWindow.Content;

                var fe = content as FrameworkElement; // Content may be an arbitrary .NET object when set using a databinding and using data templates.

                if (fe != null)
                {
                    topLevelElement = fe;
                }
                else
                {
                    // If Content is not a FrameworkElement we try to find the ContentPresenter
                    // containing the template to display the content.
                    var contentPresenter = UIHelper.FindVisualChild <ContentPresenter>(mainWindow);

                    if (contentPresenter != null && contentPresenter.Content == content)
                    {
                        // set the root element of the template as the top level element.
                        topLevelElement = (FrameworkElement)VisualTreeHelper.GetChild(contentPresenter, 0);
                    }
                }
            }

            if (topLevelElement == null)
            {
                return;
            }

            this.adorner = new BackstageAdorner(topLevelElement, this);

            var layer = AdornerLayer.GetAdornerLayer(this);

            layer.Add(this.adorner);

            layer.CommandBindings.Add(new CommandBinding(RibbonCommands.OpenBackstage,
                                                         (sender, args) =>
            {
                this.IsOpen = !this.IsOpen;
            }));
        }
Exemplo n.º 9
0
        /// <summary>
        /// When overridden in a derived class, is invoked whenever application code or internal processes call <see cref="M:System.Windows.FrameworkElement.ApplyTemplate"/>.
        /// </summary>
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            // OnAplyTemplate is executed then theme is changed
            if (adorner != null)
            {
                /*bool isOpened = IsOpen;
                 * if (isOpened)
                 * {
                 *  Hide();
                 *  IsOpen = false;
                 * }
                 * Dispatcher.BeginInvoke(DispatcherPriority.Render, (ThreadStart)(() => {
                 * adorner.Clear();
                 * adorner = null;
                 * if (isOpened)
                 * {
                 *  Dispatcher.BeginInvoke(DispatcherPriority.ApplicationIdle, (ThreadStart)(() => { IsOpen = true; }));
                 * }
                 * }));    */
                if (IsOpen)
                {
                    Hide();
                    IsOpen = false;
                    // Clear adorner
                    adorner.Clear();
                    adorner = null;

                    /*// Remove adorner
                     * AdornerLayer layer = GetAdornerLayer(this);
                     * layer.Remove(adorner);
                     * // Clear adorner
                     * adorner.Clear();
                     * adorner = null;
                     * // Create new adorner
                     * Dispatcher.BeginInvoke(DispatcherPriority.SystemIdle, (ThreadStart)(() =>
                     * {
                     *  if (adorner == null)
                     *  {
                     *      if (DesignerProperties.GetIsInDesignMode(this))
                     *      {
                     *          // TODO: in design mode it is required to use design time adorner
                     *          FrameworkElement topLevelElement = (FrameworkElement)VisualTreeHelper.GetParent(this);
                     *          double topOffset = this.TranslatePoint(new Point(0, this.ActualHeight), topLevelElement).Y;
                     *          adorner = new BackstageAdorner(topLevelElement, Content, topOffset);
                     *      }
                     *      else
                     *      {
                     *          Window mainWindow = Window.GetWindow(this);
                     *          if (mainWindow == null) return;
                     *          FrameworkElement topLevelElement = (FrameworkElement)mainWindow.Content;
                     *          if (topLevelElement == null) return;
                     *          double topOffset = this.TranslatePoint(new Point(0, this.ActualHeight), topLevelElement).Y;
                     *          adorner = new BackstageAdorner(topLevelElement, this.Content, topOffset);
                     *      }
                     *  }
                     *  layer.Add(adorner);
                     * }));*/
                }
                else
                {
                    // Clear adorner
                    adorner.Clear();
                    adorner = null;
                }
            }
        }
Exemplo n.º 10
0
        // Opens backstage on an Adorner layer
        private void Show()
        {
            if (!this.IsLoaded)
            {
                this.Loaded += this.OnDelayedShow;
                return;
            }

            if (this.Content == null)
            {
                return;
            }

            var layer = GetAdornerLayer(this);

            if (adorner == null)
            {
                if (DesignerProperties.GetIsInDesignMode(this))
                {
                    // TODO: in design mode it is required to use design time adorner
                    var topLevelElement = (FrameworkElement)VisualTreeHelper.GetParent(this);
                    var topOffset       = this.TranslatePoint(new Point(0, this.ActualHeight), topLevelElement).Y;
                    adorner = new BackstageAdorner(topLevelElement, Content, topOffset);
                }
                else
                {
                    var mainWindow = Window.GetWindow(this);
                    if (mainWindow == null)
                    {
                        return;
                    }

                    var topLevelElement = (FrameworkElement)mainWindow.Content;
                    if (topLevelElement == null)
                    {
                        return;
                    }

                    var topOffset = this.TranslatePoint(new Point(0, this.ActualHeight), topLevelElement).Y;
                    adorner = new BackstageAdorner(topLevelElement, this.Content, topOffset);
                }
            }

            layer.Add(adorner);

            var ribbon = this.FindRibbon();

            if (ribbon != null)
            {
                ribbon.TabControl.IsDropDownOpen         = false;
                ribbon.TabControl.HighlightSelectedItem  = false;
                ribbon.TabControl.RequestBackstageClose += this.OnTabControlRequestBackstageClose;

                // Disable QAT & title bar
                if (ribbon.QuickAccessToolBar != null)
                {
                    ribbon.QuickAccessToolBar.IsEnabled = false;
                }

                if (ribbon.TitleBar != null)
                {
                    ribbon.TitleBar.IsEnabled = false;
                }
            }

            var window = Window.GetWindow(this);

            if (window != null)
            {
                window.KeyDown += this.OnBackstageEscapeKeyDown;
                savedMinWidth   = window.MinWidth;
                savedMinHeight  = window.MinHeight;

                this.SaveWindowSize(window);

                if (savedMinWidth < 500)
                {
                    window.MinWidth = 500;
                }

                if (savedMinHeight < 400)
                {
                    window.MinHeight = 400;
                }

                window.SizeChanged += this.OnWindowSizeChanged;

                // We have to collapse WindowsFormsHost while Backstage is open
                this.CollapseWindowsFormsHosts(window);
            }

            var content = this.Content as IInputElement;

            if (content != null)
            {
                content.Focus();
            }
        }
Exemplo n.º 11
0
        private void DestroyAdorner()
        {
            if (this.adorner == null)
            {
                return;
            }

            var layer = AdornerLayer.GetAdornerLayer(this);
            layer.Remove(this.adorner);

            this.adorner.Clear();
            this.adorner = null;
        }
Exemplo n.º 12
0
        private void CreateAndAttachBackstageAdorner()
        {
            if (this.adorner != null)
            {
                return;
            }

            FrameworkElement topLevelElement = null;

            if (DesignerProperties.GetIsInDesignMode(this))
            {
                // TODO: in design mode it is required to use design time adorner
                topLevelElement = (FrameworkElement)VisualTreeHelper.GetParent(this);
            }
            else
            {
                var mainWindow = Window.GetWindow(this);
                if (mainWindow == null)
                {
                    return;
                }

                var content = mainWindow.Content;

                var fe = content as FrameworkElement; // Content may be an arbitrary .NET object when set using a databinding and using data templates.

                if (fe != null)
                {
                    topLevelElement = fe;
                }
                else
                {
                    // If Content is not a FrameworkElement we try to find the ContentPresenter 
                    // containing the template to display the content.
                    var contentPresenter = UIHelper.FindVisualChild<ContentPresenter>(mainWindow);

                    if (contentPresenter != null && contentPresenter.Content == content)
                    {
                        // set the root element of the template as the top level element.
                        topLevelElement = (FrameworkElement)VisualTreeHelper.GetChild(contentPresenter, 0);
                    }
                }
            }

            if (topLevelElement == null)
            {
                return;
            }

            this.adorner = new BackstageAdorner(topLevelElement, this);

            var layer = AdornerLayer.GetAdornerLayer(this);
            layer.Add(this.adorner);

            layer.CommandBindings.Add(new CommandBinding(RibbonCommands.OpenBackstage,
                (sender, args) =>
                {
                    this.IsOpen = !this.IsOpen;
                }));
        }
        private void CreateAndAttachBackstageAdorner()
        {
            // It's possible that we created an adorner but it's parent AdornerLayer got destroyed.
            // If that's the case we have to destroy our adorner.
            // This fixes #228 Backstage disappears when changing DontUseDwm
            if (this.adorner?.Parent == null)
            {
                this.DestroyAdorner();
            }

            if (this.adorner != null)
            {
                return;
            }

            FrameworkElement elementToAdorn;

            if (DesignerProperties.GetIsInDesignMode(this))
            {
                // TODO: in design mode it is required to use design time adorner
                elementToAdorn = (FrameworkElement)VisualTreeHelper.GetParent(this);
            }
            else
            {
                elementToAdorn = UIHelper.GetParent<AdornerDecorator>(this);

                if (elementToAdorn == null)
                {
                    elementToAdorn = UIHelper.GetParent<AdornerDecorator>(LogicalTreeHelper.GetParent(this));
                }
            }

            if (elementToAdorn == null)
            {
                return;
            }

            var layer = UIHelper.GetAdornerLayer(elementToAdorn);

            if (layer == null)
            {
                throw new Exception($"AdornerLayer could not be found for {this}.");
            }

            this.adorner = new BackstageAdorner(elementToAdorn, this);
            layer.Add(this.adorner);

            layer.CommandBindings.Add(new CommandBinding(RibbonCommands.OpenBackstage, HandleOpenBackstageCommandExecuted));
        }
Exemplo n.º 14
0
        // Opens backstage on an Adorner layer
        private void Show()
        {
            if (!this.IsLoaded)
            {
                this.Loaded += this.OnDelayedShow;
                return;
            }

            if (this.Content == null)
            {
                return;
            }

            var layer = GetAdornerLayer(this);
            if (adorner == null)
            {
                if (DesignerProperties.GetIsInDesignMode(this))
                {
                    // TODO: in design mode it is required to use design time adorner
                    var topLevelElement = (FrameworkElement)VisualTreeHelper.GetParent(this);
                    var topOffset = this.TranslatePoint(new Point(0, this.ActualHeight), topLevelElement).Y;
                    adorner = new BackstageAdorner(topLevelElement, Content, topOffset);
                }
                else
                {
                    var mainWindow = Window.GetWindow(this);
                    if (mainWindow == null)
                    {
                        return;
                    }

                    var topLevelElement = (FrameworkElement)mainWindow.Content;
                    if (topLevelElement == null)
                    {
                        return;
                    }

                    var topOffset = this.TranslatePoint(new Point(0, this.ActualHeight), topLevelElement).Y;
                    adorner = new BackstageAdorner(topLevelElement, this.Content, topOffset);
                }
            }

            layer.Add(adorner);

            var ribbon = this.FindRibbon();
            if (ribbon != null)
            {
                ribbon.TabControl.IsDropDownOpen = false;
                ribbon.TabControl.HighlightSelectedItem = false;
                ribbon.TabControl.RequestBackstageClose += this.OnTabControlRequestBackstageClose;

                // Disable QAT & title bar
                if (ribbon.QuickAccessToolBar != null)
                {
                    ribbon.QuickAccessToolBar.IsEnabled = false;
                }

                if (ribbon.TitleBar != null)
                {
                    ribbon.TitleBar.IsEnabled = false;
                }
            }

            var window = Window.GetWindow(this);
            if (window != null)
            {
                window.KeyDown += this.OnBackstageEscapeKeyDown;
                savedMinWidth = window.MinWidth;
                savedMinHeight = window.MinHeight;

                this.SaveWindowSize(window);

                if (savedMinWidth < 500)
                {
                    window.MinWidth = 500;
                }

                if (savedMinHeight < 400)
                {
                    window.MinHeight = 400;
                }

                window.SizeChanged += this.OnWindowSizeChanged;

                // We have to collapse WindowsFormsHost while Backstage is open
                this.CollapseWindowsFormsHosts(window);
            }

            var content = this.Content as IInputElement;
            if (content != null)
            {
                content.Focus();
            }
        }
Exemplo n.º 15
0
        /// <summary>
        /// When overridden in a derived class, is invoked whenever application code or internal processes call <see cref="M:System.Windows.FrameworkElement.ApplyTemplate"/>.
        /// </summary>
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            // OnAplyTemplate is executed then theme is changed
            if (adorner != null)
            {
                /*bool isOpened = IsOpen;
                if (isOpened)
                {
                    Hide();
                    IsOpen = false;
                }
                Dispatcher.BeginInvoke(DispatcherPriority.Render, (ThreadStart)(() => {
                adorner.Clear();
                adorner = null;
                if (isOpened)
                {
                    Dispatcher.BeginInvoke(DispatcherPriority.ApplicationIdle, (ThreadStart)(() => { IsOpen = true; }));
                }
                }));    */
                if (IsOpen)
                {
                    Hide();
                    IsOpen = false;
                    // Clear adorner
                    adorner.Clear();
                    adorner = null;
                    /*// Remove adorner
                    AdornerLayer layer = GetAdornerLayer(this);
                    layer.Remove(adorner);
                    // Clear adorner
                    adorner.Clear();
                    adorner = null;
                    // Create new adorner
                    Dispatcher.BeginInvoke(DispatcherPriority.SystemIdle, (ThreadStart)(() =>
                    {
                        if (adorner == null)
                        {
                            if (DesignerProperties.GetIsInDesignMode(this))
                            {
                                // TODO: in design mode it is required to use design time adorner
                                FrameworkElement topLevelElement = (FrameworkElement)VisualTreeHelper.GetParent(this);
                                double topOffset = this.TranslatePoint(new Point(0, this.ActualHeight), topLevelElement).Y;
                                adorner = new BackstageAdorner(topLevelElement, Content, topOffset);
                            }
                            else
                            {
                                Window mainWindow = Window.GetWindow(this);
                                if (mainWindow == null) return;
                                FrameworkElement topLevelElement = (FrameworkElement)mainWindow.Content;
                                if (topLevelElement == null) return;
                                double topOffset = this.TranslatePoint(new Point(0, this.ActualHeight), topLevelElement).Y;
                                adorner = new BackstageAdorner(topLevelElement, this.Content, topOffset);
                            }
                        }
                        layer.Add(adorner);
                    }));*/
                }
                else
                {
                    // Clear adorner
                    adorner.Clear();
                    adorner = null;
                }
            }
        }