Как установить текст во главе RibbonApplicationMenu
Я пытаюсь получить текст на верхнем уровне RibbonApplicationMenu (пытаясь получить слово "Файл", похожее на Word или Outlook). Кажется, Microsoft.Windows.Controls.Ribbon.RibbonApplicationMenu
http://msdn.microsoft.com/en-us/library/microsoft.windows.controls.ribbon.ribbonapplicationmenu.aspx поддерживает объект SmallImageSource, но не имеет свойства text. Установка свойства Label
не работает для этой проблемы.
xmlns:ribbon="clr-namespace:Microsoft.Windows.Controls.Ribbon;assembly=RibbonControlsLibrary"
<ribbon:RibbonApplicationMenu Label="File"> <!--doesn't set the label -->
</ribbon:RibbonApplicationMenu>
Цель состоит в том, чтобы слово "Файл" появилось в приведенной ниже области.
![RibbonApplicationMenu]()
Ответы
Ответ 1
Простейшим решением (для меня) было вставить DrawingImage с GlyphRun внутри. На отдельном сообщении задается вопрос о том, как получить AdvanceWidths и GlyphIndicies для GlyphRun. Результат ниже
<ribbon:RibbonApplicationMenu.SmallImageSource>
<DrawingImage>
<DrawingImage.Drawing>
<GlyphRunDrawing ForegroundBrush="White">
<GlyphRunDrawing.GlyphRun>
<GlyphRun
CaretStops="{x:Null}"
ClusterMap="{x:Null}"
IsSideways="False"
GlyphOffsets="{x:Null}"
GlyphIndices="41 76 79 72"
FontRenderingEmSize="12"
DeviceFontName="{x:Null}"
AdvanceWidths="5.859375 2.90625 2.90625 6.275390625">
<GlyphRun.GlyphTypeface>
<GlyphTypeface FontUri="C:\WINDOWS\Fonts\SEGOEUI.TTF"/>
</GlyphRun.GlyphTypeface>
</GlyphRun>
</GlyphRunDrawing.GlyphRun>
</GlyphRunDrawing>
</DrawingImage.Drawing>
</DrawingImage>
</ribbon:RibbonApplicationMenu.SmallImageSource>
Результирующая лента:
![GlyphRun Result]()
Ответ 2
Удалите ненужные элементы для визуального дерева и замените их на TextBlock, который берет текст из свойства Label. Вы должны сделать это как для кнопки на основном визуальном дереве, так и во всплывающем визуальном дереве. Наконец, поскольку текст более сложный, чем типичный образ, полезно отступить от эффектов подсветки aero.
Чтобы использовать следующий код, назначьте имя в меню приложения в XAML и вызовите ReplaceRibbonApplicationMenuButtonContent
с ним из обработчика событий Loaded окна.
/// <summary>
/// Replaces the image and down arrow of a Ribbon Application Menu Button with the button 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);
}
Ответ 3
Right. Если вы не хотите компиляции кода и сложных вычислений глифов, используйте следующий XAML:
<RibbonApplicationMenu.SmallImageSource>
<DrawingImage>
<DrawingImage.Drawing>
<GeometryDrawing>
<GeometryDrawing.Geometry>
<RectangleGeometry Rect="0,0,20,20"></RectangleGeometry>
</GeometryDrawing.Geometry>
<GeometryDrawing.Brush>
<VisualBrush Stretch="Uniform">
<VisualBrush.Visual>
<TextBlock Text="File" FontSize="16" Foreground="White" />
</VisualBrush.Visual>
</VisualBrush>
</GeometryDrawing.Brush>
</GeometryDrawing>
</DrawingImage.Drawing>
</DrawingImage>
</RibbonApplicationMenu.SmallImageSource>
Преимущества такого подхода:
- XAML-only, без кода-кода
- Нет измерения глифов
- Легкая замена ярлыка
Ответ 4
Tricky! Возможно, вам придется заменить PART_ToggleButton шаблона своей собственной версией, чтобы иметь возможность устанавливать текст.
Использование WPF Vizualizer показывает, что шаблон содержит StackPanel с изображением и контуром (DownArrow), но без TextBlock, поэтому я сомневаюсь, что в текущем элементе есть место для указания текста метки.
Конечно, вы также можете создать образ, содержащий нужный текст.
Ответ 5
Другой способ сделать это - просто использовать сетку и нарисовать TextBlock в нужном месте. Обязательно сделайте TextBlock NOT HitTestVisible.
<Grid>
<DockPanel>
<ribbon:Ribbon DockPanel.Dock="Top">
<!-- your ribbon stuff -->
</ribbon:Ribbon>
<!-- your other stuff -->
</DockPanel>
<TextBlock Margin="3,26" Foreground="White"
IsHitTestVisible="False"
Text="{LocalizeExtension:LocText Key=FILE, Dict=Strings, Assembly=YourAssembly}"/>
</Grid>
Преимущества:
- меньше xaml
- проще локализовать
Неудобство:
- выглядит хуже в Windows XP
Ответ 6
Следующее решение было размещено на форуме MSDN. Это включает в себя изменение стиля, используемого в теме по умолчанию (?).
Я проверил исходный код элементов управления ленты (пожалуйста, загрузите MicrosoftRibbonForWPFSourceAndSamples с веб-сайта). В файле темы (\MicrosoftRibbonForWPFSourceAndSamples\RibbonControlsLibrary\Themes\Generic.xaml
) ленты вы можете найти, что этот стиль " Ü
" используется для RibbonApplicationMenu
. В этом стиле нет элемента для отображения текста, он имеет только один элемент изображения для отображения изображения.
К счастью, мы могли бы изменить код стиля и добавить некоторые элементы управления в стиле " Ü
". Пожалуйста, ниже код:
строка 7264, измените код:
<!--<Image IsHitTestVisible="False"
Source="{Binding RelativeSource ={RelativeSource FindAncestor, AncestorType ={x:Type ribbon:RibbonApplicationMenu}},
Path = SmallImageSource} "HorizontalAlignment =" Center "VerticalAlignment =" Center "Width =" 16 "Height =" 16 "RenderOptions.BitmapScalingMode =" NearestNeighbor "RenderOptions.EdgeMode =" Aliased "/> ->
строка 7433, добавьте код Label="{TemplateBinding Label}"
в конец элемента RibbonToggleButton
:
......
<ControlTemplate TargetType="{x:Type ribbon:RibbonApplicationMenu}">
<Grid Focusable="False"
x:Name="OuterGrid"
SnapsToDevicePixels="True">
<ribbon:RibbonToggleButton x:Name="PART_ToggleButton"
BorderBrush="{TemplateBinding BorderBrush}"
Background="{TemplateBinding Background}"
BorderThickness="{TemplateBinding BorderThickness}"
Style="{StaticResource Ü}"
FocusVisualStyle="{TemplateBinding FocusVisualStyle}"
Height="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Height}"
Width="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Width}"
ToolTipTitle="{TemplateBinding ToolTipTitle}"
ToolTipDescription="{TemplateBinding ToolTipDescription}"
ToolTipImageSource="{TemplateBinding ToolTipImageSource}"
ToolTipFooterTitle="{TemplateBinding ToolTipFooterTitle}"
ToolTipFooterDescription="{TemplateBinding ToolTipFooterDescription}"
ToolTipFooterImageSource="{TemplateBinding ToolTipFooterImageSource}"
SmallImageSource="{TemplateBinding SmallImageSource}"
IsChecked="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=IsDropDownOpen, Mode=TwoWay}"
Label="{TemplateBinding Label}"/>
строка 7564, добавьте код Label="{TemplateBinding Label}"
в конце элемента RibbonToggleButton
:
......
<Canvas>
<ribbon:RibbonToggleButton x:Name="PART_PopupToggleButton"
AutomationProperties.Name="{Binding RelativeSource={RelativeSource TemplatedParent},
Path = (AutomationProperties.Name)} "Canvas.Top =" -24 "Canvas.Left =" 3 "IsChecked =" {Binding RelativeSource = {RelativeSource TemplatedParent}, Path = IsDropDownOpen} "BorderBrush =" {TemplateBinding BorderBrush} " Background = "{TemplateBinding Background}" BorderThickness = "{TemplateBinding BorderThickness}"
Style = "{StaticResource Ü}" Focusable = "False" Height = "{Binding RelativeSource = {RelativeSource TemplatedParent}, Path = Высота}" Width = "{Binding RelativeSource = {RelativeSource TemplatedParent}, Path = Width}" Label = " {TemplateBinding Label} "/> И в RibbonWindow мы могли бы установить свойство Label RibbonApplicationMenu как:
<ribbon:RibbonApplicationMenu Label="File">
Сообщение на форуме содержало ZIP с измененными источниками, но ссылка больше не работает.