Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
105
Logo in ApplicationMenu
posted

Hi.

Adding own logo to Ribbon is simple. But, how to change image for every state of button? I want to do this, because my logo looks horrible on the orange background (under "Pressed" state).

I've tried to retemplate the entire AppMenu (the object "ApplicationMenuPresenter"). I've copied proper style from RibbonGeneric.xaml, but finaly I've gave up with this solution. There must be some simpler way to do this. I just want to put some images in coresponding grids (Normal, Hover, Pressed).

Any help would be appreciated

Lukasz.

Parents
  • 54837
    Verified Answer
    Offline posted

    There is only 1 Image property and that is the one used in our template. You could possibly retemplate the ApplicationMenuPresenter or even the ApplicationMenu itself to set your image property based on triggers but then you would have to use that as a local style/template which would mean the Theme property couldn't affect the element. One clean way to accomplish this would be to have a custom derived ApplicationMenu class that adds 2 new ImageSource properties and then you use those to coerce the Image property based on the state of the application menu.
    e.g.

    public class ApplicationMenuEx : ApplicationMenu
    {
        #region Constructor
        static ApplicationMenuEx()
        {
            ApplicationMenu.ImageProperty.OverrideMetadata(typeof(ApplicationMenuEx), new FrameworkPropertyMetadata(nullnew CoerceValueCallback(CoerceImage)));
        } 
        #endregion //Constructor
     
        #region Properties
     
        #region HoverImage
     
        /// <summary>
        /// Identifies the <see cref="HoverImage"/> dependency property
        /// </summary>
        public static readonly DependencyProperty HoverImageProperty = DependencyProperty.Register("HoverImage",
            typeof(ImageSource), typeof(ApplicationMenuEx), new FrameworkPropertyMetadata(nullnew PropertyChangedCallback(OnOtherImageChanged)));
     
        /// <summary>
        /// Returns or sets the ImageSource used when the mouse hovers over the application menu.
        /// </summary>
        /// <seealso cref="HoverImageProperty"/>
        public ImageSource HoverImage
        {
            get
            {
                return (ImageSource)this.GetValue(ApplicationMenuEx.HoverImageProperty);
            }
            set
            {
                this.SetValue(ApplicationMenuEx.HoverImageProperty, value);
            }
        }
     
        #endregion //HoverImage
     
        #region PressedImage
     
        /// <summary>
        /// Identifies the <see cref="PressedImage"/> dependency property
        /// </summary>
        public static readonly DependencyProperty PressedImageProperty = DependencyProperty.Register("PressedImage",
            typeof(ImageSource), typeof(ApplicationMenuEx), new FrameworkPropertyMetadata(nullnew PropertyChangedCallback(OnOtherImageChanged)));
     
        private static void OnOtherImageChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ((ApplicationMenuEx)d).CoerceValue(ApplicationMenu.ImageProperty);
        }
     
        /// <summary>
        /// Returns or sets the ImageSource used when the application menu is open.
        /// </summary>
        /// <seealso cref="PressedImageProperty"/>
        public ImageSource PressedImage
        {
            get
            {
                return (ImageSource)this.GetValue(ApplicationMenuEx.PressedImageProperty);
            }
            set
            {
                this.SetValue(ApplicationMenuEx.PressedImageProperty, value);
            }
        }
     
        #endregion //PressedImage
     
        #endregion //Properties
     
        #region Methods
        private static object CoerceImage(DependencyObject d, object value)
        {
            ApplicationMenuEx menu = (ApplicationMenuEx)d;
     
            if (menu.IsOpen && menu.PressedImage != null)
                return menu.PressedImage;
     
            if (menu.IsMouseOver && menu.HoverImage != null)
                return menu.HoverImage;
     
            return value;
        }
        #endregion //Methods
     
        #region Base class overrides
        protected override void OnMouseEnter(MouseEventArgs e)
        {
            base.OnMouseEnter(e);
     
            this.CoerceValue(ImageProperty);
        }
     
        protected override void OnMouseLeave(MouseEventArgs e)
        {
            base.OnMouseLeave(e);
     
            this.CoerceValue(ImageProperty);
        }
     
        protected override void OnOpened(RoutedEventArgs args)
        {
            base.OnOpened(args);
     
            this.CoerceValue(ImageProperty);
        }
     
        protected override void OnClosed(RoutedEventArgs args)
        {
            base.OnClosed(args);
     
            this.CoerceValue(ImageProperty);
        } 
        #endregion //Base class overrides
    }

     

Reply Children