Search code examples
wpfribbon-control

RibbonControl: center Title


I'm using the Microsoft RibbonControl on a UserControl (it needs to be so we can host it on a stub form to host the WPF in our MDI system). Sadly, the title of the Ribbon displays Top/Left in the ribbon's header, and it looks ridiculous. How do I get at that sucker?


Solution

  • I am working on just about the same thing right now. I solved it by using a datatemplate for the ribbon title:

    <r:Ribbon.TitleTemplate>
        <DataTemplate>
            <TextBlock Text="Put you title here" Margin="3,3,0,0"></TextBlock>
        </DataTemplate>
    </r:Ribbon.TitleTemplate>
    

    If the ribbon is used in a RibbonWindow, you probably also want to add a glow to the title text to be able to read it properly when placed over a dark background. In that case, add this XAML inside the TextBlock:

    <TextBlock.BitmapEffect>
        <OuterGlowBitmapEffect GlowColor="White" Opacity="0.7" GlowSize="10"/>
    </TextBlock.BitmapEffect>
    

    There is one more problem with the Ribbon when used within a RibbonWindow; the title text will either be placed correctly when window state is Normal or when window is maximized. To solve this I bound the TextBlock Margin to a property in the codebind:

    public Thickness TitleMargin
    {
        get { return this.WindowState == WindowState.Maximized ? new Thickness(0, 3, 0, 0) : new Thickness(0); }
    }
    

    To get this working, you also need to fire a PropertyChanged event each time the window state changes:

    protected override void OnStateChanged(EventArgs e)
    {
        OnPropertyChanged("TitleMargin");
        base.OnStateChanged(e);
    }