How to set text at the head of a RibbonApplicationMenu

后端 未结 6 2069
Happy的楠姐
Happy的楠姐 2020-12-29 23:20

I\'m trying to have text in the top level of a RibbonApplicationMenu (trying to the get the word \"File\" there similar to Word or Outlook). It seems the Microsoft.Wi

6条回答
  •  我在风中等你
    2020-12-29 23:55

    Remove the unwanted elements for the visual tree, and replace them with a TextBlock that takes the text from the Label property. You have to do this for both the button on the main visual tree and on the popup's visual tree. Finally, since text is more intricate than the typical image, it is helpful to back off on the aero highlighting effects.

    To use the following code, assign a name to the application menu in the XAML and call ReplaceRibbonApplicationMenuButtonContent with it from the Loaded event handler of the window.

    /// 
    /// Replaces the image and down arrow of a Ribbon Application Menu Button with the button's Label text.
    /// 
    /// The menu whose application button should show the label text.
    /// 
    /// The method assumes the specific visual tree implementation of the October 2010 version of .
    /// Fortunately, since the application menu is high profile, breakage due to version changes should be obvious.
    /// Hopefully, native support for text will be added before the implementation breaks.
    /// 
    void ReplaceRibbonApplicationMenuButtonContent(RibbonApplicationMenu menu)
    {
        Grid outerGrid = (Grid)VisualTreeHelper.GetChild(menu, 0);
        RibbonToggleButton toggleButton = (RibbonToggleButton)outerGrid.Children[0];
        ReplaceRibbonToggleButtonContent(toggleButton, menu.Label);
    
        Popup popup = (Popup)outerGrid.Children[2];
        EventHandler popupOpenedHandler = null;
        popupOpenedHandler = new EventHandler(delegate
        {
            Decorator decorator = (Decorator)popup.Child;
            Grid popupGrid = (Grid)decorator.Child;
            Canvas canvas = (Canvas)popupGrid.Children[1];
            RibbonToggleButton popupToggleButton = (RibbonToggleButton)canvas.Children[0];
            ReplaceRibbonToggleButtonContent(popupToggleButton, menu.Label);
            popup.Opened -= popupOpenedHandler;
        });
        popup.Opened += popupOpenedHandler;
    }
    
    void ReplaceRibbonToggleButtonContent(RibbonToggleButton toggleButton, string text)
    {
        // Subdues the aero highlighting to that the text has better contrast.
        Grid grid = (Grid)VisualTreeHelper.GetChild(toggleButton, 0);
        Border middleBorder = (Border)grid.Children[1];
        middleBorder.Opacity = .5;
    
        // Replaces the images with the label text.
        StackPanel stackPanel = (StackPanel)grid.Children[2];
        UIElementCollection children = stackPanel.Children;
        children.RemoveRange(0, children.Count);
        TextBlock textBlock = new TextBlock(new Run(text));
        textBlock.Foreground = Brushes.White;
        children.Add(textBlock);
    }
    

提交回复
热议问题