如何在 RibbonApplicationMenu 的顶部设置文本

发布于 2024-11-24 06:57:44 字数 756 浏览 3 评论 0原文

我试图在 RibbonApplicationMenu 的顶层添加文本(尝试在此处获取单词 File ,类似于 Word 或 Outlook)。似乎是 Microsoft.Windows.Controls.Ribbon.RibbonApplicationMenu

MSDN

支持 SmallImageSource 但不支持文本属性。设置 Label 属性无法解决此问题。

xmlns:ribbon="clr-namespace:Microsoft.Windows.Controls.Ribbon;assembly=RibbonControlsLibrary"    
<ribbon:RibbonApplicationMenu Label="File"><!--doesn't set the label -->
</ribbon:RibbonApplicationMenu>

目标是让“文件”一词出现在下面的圆圈区域中。

RibbonApplicationMenu

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.Windows.Controls.Ribbon.RibbonApplicationMenu

MSDN

supports a SmallImageSource but no text property. Setting the Label property doesn't work for this problem.

xmlns:ribbon="clr-namespace:Microsoft.Windows.Controls.Ribbon;assembly=RibbonControlsLibrary"    
<ribbon:RibbonApplicationMenu Label="File"><!--doesn't set the label -->
</ribbon:RibbonApplicationMenu>

The goal is to have the word "File" appear in the circled area below.

RibbonApplicationMenu

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

缪败 2024-12-01 06:57:44

最简单的解决方案(对我来说)是插入 DrawingImage 带有 GlyphRun 内。在单独的帖子上询问如何获取 GlyphRun 的 AdvanceWidths 和 GlyphIndicies 。结果位于

<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

The simplest solution (to me) was to insert a DrawingImage with a GlyphRun inside. On a separate post is asked how to get the AdvanceWidths and GlyphIndicies for the GlyphRun. The result is below

<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>

Resulting Ribbon:

GlyphRun Result

撩起发的微风 2024-12-01 06:57:44

删除可视化树中不需要的元素,并将它们替换为从 Label 属性获取文本的 TextBlock。您必须对主视觉树上的按钮和弹出窗口的视觉树上的按钮执行此操作。最后,由于文本比典型图像更复杂,因此减少航空突出效果会很有帮助。

要使用以下代码,请在 XAML 中为应用程序菜单分配一个名称,并从窗口的 Loaded 事件处理程序中使用该名称调用 ReplaceRibbonApplicationMenuButtonContent

/// <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);
}

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.

/// <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);
}
纸伞微斜 2024-12-01 06:57:44

正确的。如果您不需要隐藏代码且不需要复杂的字形计算,请使用以下 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,无代码隐藏
  • 无字形测量
  • 易于更改标签

Right. If you want no code-behind and no complex glyph computations, use the following 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>

Advantages of this approach:

  • XAML-only, no code-behind
  • No glyph measurement
  • Easy to change label
败给现实 2024-12-01 06:57:44

棘手!您可能必须将模板的 PART_ToggleButton 替换为您自己的版本才能设置文本。

使用WPF Vizualizer显示模板包含一个带有图像和路径(DownArrow)的StackPanel,但没有TextBlock,所以我怀疑当前控件中有一个地方可以指定标签文本。

当然,您也可以创建包含所需文本的图像

Tricky! You might have to replace the PART_ToggleButton of the template with your own version to be able to set the text.

Using the WPF Vizualizer shows that the template contains a StackPanel with an Image and a Path (DownArrow), but no TextBlock, so I doubt that there is a place in the current control to specify the label text.

Of course, you could also create an image that contains the desired text.

蓬勃野心 2024-12-01 06:57:44

另一种方法是使用网格并在正确的位置绘制 TextBlock。请务必使 TextBlock 不为​​ 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 上看起来不太好

Another way to do so is just using a grid and paint a TextBlock at the right place. Be sure to make the 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>

Advantages:

  • less xaml
  • way easier to localize

Disadvantage:
- looks less nice on Windows XP

情深已缘浅 2024-12-01 06:57:44

发布了以下解决方案 MSDN 论坛。它涉及更改默认(?)主题中使用的样式。

我检查了Ribbon控件的源代码(请下载
来自网站的 MicrosoftRibbonForWPFSourceAndSamples )。在
主题文件
(<代码>\MicrosoftRibbonForWPFSourceAndSamples\RibbonControlsLibrary\Themes\Generic.xaml)
的功能区,你会发现这种样式“Ü”用于
RibbonApplicationMenu。在这种风格中,没有任何元素可以显示
Text,它只有一个 Image 元素来显示图像。

幸运的是,我们可以修改样式代码并添加一些控件
Ü”样式。请输入以下代码:

第7264行,更改代码:

 

第7433行,在末尾添加代码Label="{TemplateBinding Label}"
RibbonToggleButton 元素:

<前><代码>......

<网格焦点=“假”
x:名称=“外部网格”
SnapsToDevicePixels="True">
<功能区:RibbonToggleButton x:Name="PART_ToggleButton"
BorderBrush =“{TemplateBinding BorderBrush}”
背景=“{TemplateBinding背景}”
BorderThickness =“{TemplateBinding BorderThickness}”
样式 =“{StaticResource Ü}”
FocusVisualStyle="{TemplateBinding FocusVisualStyle}"
高度=“{绑定RelativeSource = {RelativeSource TemplatedParent},路径=高度}”
宽度=“{绑定RelativeSource={RelativeSource TemplatedParent},路径=宽度}”
ToolTipTitle="{模板绑定ToolTipTitle}"
ToolTipDescription="{模板绑定ToolTipDescription}"
ToolTipImageSource="{模板绑定ToolTipImageSource}"
ToolTipFooterTitle="{模板绑定ToolTipFooterTitle}"
ToolTipFooterDescription="{TemplateBinding ToolTipFooterDescription}"
ToolTipFooterImageSource="{TemplateBinding ToolTipFooterImageSource}"
SmallImageSource="{TemplateBinding SmallImageSource}"
IsChecked =“{绑定RelativeSource = {RelativeSource TemplatedParent},路径= IsDropDownOpen,模式= TwoWay}”
标签=“{TemplateBinding标签}”/>

第7564行,在末尾添加代码Label="{TemplateBinding Label}"
RibbonToggleButton 元素:

<前><代码>......
<画布>
<功能区:RibbonToggleButton x:Name="PART_PopupToggleButton"
AutomationProperties.Name =“{绑定RelativeSource = {RelativeSource TemplatedParent},

路径=(AutomationProperties.Name)}"
画布.Top =“-24”
画布.左=“3”
IsChecked =“{绑定RelativeSource = {RelativeSource TemplatedParent},Path = IsDropDownOpen}”
BorderBrush =“{TemplateBinding BorderBrush}”
背景=“{TemplateBinding背景}”
BorderThickness="{TemplateBinding BorderThickness}"
样式=“{StaticResource Ü}”
可聚焦=“假”
高度=“{绑定RelativeSource={RelativeSource TemplatedParent},路径=高度}”
宽度=“{绑定RelativeSource={RelativeSource TemplatedParent},路径=宽度}”
标签=“{TemplateBinding标签}”/>
在 RibbonWindow 中,我们可以将 RibbonApplicationMenu 的 Label 属性设置为:


论坛帖子确实包含修改后的源的 ZIP,但链接不再有效。

The following solution was posted on an MSDN forum. It involves altering the style used in the default (?) theme.

I checked the source code of the Ribbon controls (please download the
MicrosoftRibbonForWPFSourceAndSamples from web site). In the
theme file
(\MicrosoftRibbonForWPFSourceAndSamples\RibbonControlsLibrary\Themes\Generic.xaml)
of the ribbon, you could find this style "Ü" is used to the
RibbonApplicationMenu. In this style, there is no element to display
the Text, it only has one Image element to display the image.

Fortunately, we could modify the style code and add some controls in
the "Ü" style. Please below code:

line 7264, change the code:

 <!--<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" />-->

line 7433, add code Label="{TemplateBinding Label}" in the end of
the RibbonToggleButton element:

 ......
 <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}"/>

line 7564, add code Label="{TemplateBinding Label}" in the end of
the RibbonToggleButton element:

......
<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=Height}"
Width="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Width}"
Label="{TemplateBinding Label}"/>
And in the RibbonWindow, we could set the Label property of the RibbonApplicationMenu as:

<ribbon:RibbonApplicationMenu Label="File">

The forum post did include a ZIP of the modified sources, but the link no longer works.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文