简体   繁体   中英

Programmatically hide WPF Ribbon Header

I am using VS2010's WPF Ribbon Application. Each RibbonGroup has a Header. Even If I leave the Header empty, the Ribbon will still reserve an empty space for the Header. How can I programmatically hide the header?

For instance, I have following Xaml:

<ribbon:RibbonTab x:Name="HelpTab"
                    Header="Help" FontSize="10">
    <ribbon:RibbonGroup x:Name="HelpGroup"
                        Header="Help Group" FontFamily="Verdana" FontWeight="Bold">
             <!-- ..... -->
        </ribbon:RibbonButton>
    </ribbon:RibbonGroup>
</ribbon:RibbonTab>
</ribbon:Ribbon>

I want to programmatically hide the part (header text and height space) marked by red rectangle.

在此处输入图像描述

I'm looking for a C# code behind solution where I could hide the text and the space (height) the header takes up all together, something such as below:

// of course, this doesn't work    
HelpTab.HeaderStyle.Visibility = Visibility.Hide

You can do it via the VisualTreeHelper . Just go set the row MinHeight to 0:

private void RibbonLoaded(object sender, RoutedEventArgs e)
{
  DependencyObject groupBorder = VisualTreeHelper.GetChild(Foobar, 0);
  Grid groupMainGrid = VisualTreeHelper.GetChild(groupBorder , 0) as Grid;
  if (groupMainGrid != null)
  {
    groupMainGrid.RowDefinitions[2].MinHeight = 0;
  }
} 

This is assuming that you didn't set the Header property. The height of the row is set by default to Auto . So if you set the Header property, you might as well set the Height to 0:

groupMainGrid.RowDefinitions[2].Height = 0;

You can always create stack panel instead of ribbon group.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM