2011-07-14 15 views
21

RibbonApplicationMenuの最上位にテキストを配置しようとしています(WordまたはOutlookに似た「ファイル」という単語を取得しようとしています)。 Microsoft.Windows.Controls.Ribbon.RibbonApplicationMenuhttp://msdn.microsoft.com/en-us/library/microsoft.windows.controls.ribbon.ribbonapplicationmenu.aspxはSmallImageSourceをサポートしていますが、テキストプロパティはサポートされていないようです。 Labelプロパティを設定してもこの問題は発生しません。RibbonApplicationMenuの先頭にテキストを設定する方法

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

</ribbon:RibbonApplicationMenu> 

目標は、下の円の領域に「ファイル」という単語を表示することです。

+5

Microsoftはこれを修正する必要があります。 – 00jt

答えて

21

(私にとって)最も簡単な解決策は、内部GlyphRunDrawingImageを挿入することでした。 separate postで、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

+0

しかし、あなたはどのようにしてそれを自分のカスタムテキストにするのですか? は、私が試した: GlyphIndices = "43 72 79 79 82 3 58 82 85 79 71" AdvanceWidths = "9.62666666666667 7.41333333333333 2.96 2.96 7.41333333333333 3.70666666666667 12.5866666666667 7.41333333333333 4.44 2.96 7.41333333333333" そして、それは "Hello World" の..ですが、超小型です。 – 00jt

+6

これは明らかに修正する必要があるものです。それはちょうど:Label = "File" – 00jt

+7

@ 00jt +1である必要があります。私はこの不潔なものを置く必要があると信じることはできません(私は答えが良くないと言っているわけではありませんが、実際にはそれはすごく洗練されていると思います。 – MasterMastic

2

トリッキー!テキストを設定するには、テンプレートのPART_ToggleButtonを独自のバージョンに置き換える必要があります。

WPF Vizualizerを使用すると、イメージとパス(DownArrow)がTextBlockがないStackPanelがテンプレートに含まれていることがわかります。そのため、現在のコントロールにラベルテキストを指定する場所がないと思われます。

もちろん、でも、希望のテキストを含む画像を作成することができます。

+0

私は、テキスト「ファイル」にちょっとしたハックが含まれている画像を作成することを検討しましたが、おそらくうまくいくでしょう。 –

+0

ハックの多くの弱点に注目する価値があります:96以外のDPIではテキストがぼやけて見えます。 ClearTypeのテキスト調整は機能しません。ローカライズが難しい。スクリーンリーダーでは価値がありません。 –

13

ビジュアルツリーの不要な要素を削除し、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); 
} 
+1

これまでのところ、これは受け入れられた解決策でなければなりません... –

1

これだけのグリッドを使用して行い、適切な場所でのTextBlockをペイントする別の方法。 TextBlockをHitTestVisibleにしないでください。 - Windows XPの

+1

私はあなたのオプションを試しましたが、RibbonApplicationMenuをクリックすると、テキストが隠されています。ドロップダウンメニューが表示されていない場合は「ファイル」と表示されます。 – 00jt

+0

あなたはそれについて正しいです...あなたは再び登場するRibbonMenuでTextBlockを使うことができましたが、私は個人的に非常に重要であるとは考えていませんでした –

0

右にはあまり良さそうに見えます:

<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
  • 方法簡単には

デメリットをローカライズします。このアプローチの

<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のみ、ノーコードビハインド
  • ませんグリフ測定あなたは何のコードビハインドと複雑なグリフの計算をしたいない場合は、以下のXAMLを使用します
  • ラベルを簡単に変更する
関連する問題