Xóa các phần tử không mong muốn cho cây thị giác và thay thế chúng bằng một TextBlock lấy văn bản từ thuộc tính Nhãn. Bạn phải làm điều này cho cả hai nút trên cây thị giác chính và trên cây thị giác của cửa sổ bật lên. Cuối cùng, vì văn bản phức tạp hơn so với hình ảnh thông thường, nên hữu ích khi tắt các hiệu ứng làm nổi bật aero.
Để sử dụng mã sau, hãy gán tên cho menu ứng dụng trong XAML và gọi ReplaceRibbonApplicationMenuButtonContent
với nó từ trình xử lý sự kiện được tải của cửa sổ.
/// <summary>
/// Replaces the image and down arrow of a Ribbon Application Menu Button with the button's Label text.
/// </summary>
/// <param name="menu">The menu whose application button should show the label text.</param>
/// <remarks>
/// The method assumes the specific visual tree implementation of the October 2010 version of <see cref="RibbonApplicationMenu"/>.
/// 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.
/// </remarks>
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);
}
Nguồn
2011-07-18 11:14:17
Microsoft cần sửa lỗi này. – 00jt