如何隐藏WPF功能区中除应用程序菜单按钮以外的所有内容

时间:2013-03-02 17:39:55

标签: c# wpf ribbon

当我在WPF中添加功能区时,我得到一个完整的功能区控件。如何隐藏除应用程序菜单按钮以外的所有内容?我可以做变通办法但是有没有标准方法可以隐藏其他面板?

enter image description here

我懒得设计自己的控件

1 个答案:

答案 0 :(得分:0)

您可以在Loaded事件中清除项目并将功能区高度属性设置为45。

   <ribbon:Ribbon x:Name="Ribbon" Loaded="Ribbon_Loaded">
        <ribbon:Ribbon.ApplicationMenu>
            <ribbon:RibbonApplicationMenu SmallImageSource="Images\SmallIcon.png">
                <ribbon:RibbonApplicationMenuItem Header="Hello _Ribbon"
                                                  x:Name="MenuItem1"
                                                  ImageSource="Images\LargeIcon.png"/>
            </ribbon:RibbonApplicationMenu>
        </ribbon:Ribbon.ApplicationMenu>
        <ribbon:RibbonTab x:Name="HomeTab" 
                          Header="Home">
            <ribbon:RibbonGroup x:Name="Group1" 
                                Header="Group1">
                <ribbon:RibbonButton x:Name="Button1"
                                     LargeImageSource="Images\LargeIcon.png"
                                     Label="Button1" />

                <ribbon:RibbonButton x:Name="Button2"
                                     SmallImageSource="Images\SmallIcon.png"
                                     Label="Button2" />
                <ribbon:RibbonButton x:Name="Button3"
                                     SmallImageSource="Images\SmallIcon.png"
                                     Label="Button3" />
                <ribbon:RibbonButton x:Name="Button4"
                                     SmallImageSource="Images\SmallIcon.png"
                                     Label="Button4" />                    
            </ribbon:RibbonGroup>                
        </ribbon:RibbonTab>
    </ribbon:Ribbon> 

代码隐藏:

private void Ribbon_Loaded(object sender, RoutedEventArgs e)
{
    Ribbon menu = sender as Ribbon;
    menu.Height = 45;
    menu.Items.Clear();
}

您可以在功能区外控制中单独使用它,但在这种情况下,您将在下拉菜单中丢失样式:

<Grid x:Name="LayoutRoot">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>

    <ribbon:RibbonApplicationMenu SmallImageSource="Images\SmallIcon.png" HorizontalAlignment="Left">
        <ribbon:RibbonApplicationMenuItem Header="Hello _Ribbon"
                                                  x:Name="MenuItem9"
                                                  ImageSource="Images\LargeIcon.png"                                              
                                          />
    </ribbon:RibbonApplicationMenu>

</Grid>                                     
                                          />
    </ribbon:RibbonApplicationMenu>