WPF更改menuitem的大小

时间:2012-06-12 20:45:15

标签: c# wpf

我有一个在MVVM中制作的菜单,但我的菜单很大,如图所示 http://i49.tinypic.com/wi1vk3.png

所以在我的主窗口中,我有一个菜单,它绑定到一个菜单列表。

在我的视图中

 <Menu VerticalAlignment="Top">
       <MenuItem Header="File" ItemsSource="{Binding MenuList}" />
    </Menu>

在我的ViewModel中

List<MenuViewModelBase> _menuList = new List<MenuViewModelBase>();
_menuList.Add(new MenuViewModel("New", NewCommand));
_menuList.Add(new MenuViewModel("Open", OpenCommand));
_menuList.Add(new MenuViewModel("Save", SaveCommand));
_menuList.Add(new MenuViewModel("Exit", ExitCommand));

我的MenuView是

<MenuItem>
  <MenuItem.Resources>
    <Style TargetType="MenuItem">
      <Setter Property="Header" Value="{Binding Name}" />
      <Setter Property="Command" Value="{Binding ActionCommand}" />
    </Style>
  </MenuItem.Resources>
</MenuItem>

有人可以告诉我如何调整菜单项的大小以匹配其内容吗?

1 个答案:

答案 0 :(得分:0)

我相信你所看到的是default template为InputGestureText预留的空间。如果您想要删除该区域,我相信您最好的选择是为菜单项实现自己的模板。使用默认模板作为起点,修改它以获得您正在寻找的内容应该相当容易。

<强>更新

我已经包含了一个示例,其中我从上面的链接复制了MenuItem样式并将其添加到我的项目中。然后我从SubMenuHeaderTemplate中删除了Icon和InputGesture区域。通常,如果您看到要更改的默认行为,则可以尝试通过样式进行更改,如果不起作用,则可以使用Microsoft提供的模板作为基础来覆盖默认模板。

XAML:

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow">
<Window.Resources>

    <Color x:Key="DisabledForegroundColor">#FF888888</Color>
    <Color x:Key="ControlLightColor">White</Color>
    <Color x:Key="ControlMediumColor">#FF7381F9</Color>
    <Color x:Key="ControlMouseOverColor">#FF3843C4</Color>

    <!--Border colors-->
    <Color x:Key="BorderMediumColor">#FF888888</Color>

    <LinearGradientBrush x:Key="MenuPopupBrush" EndPoint="0.5,1" StartPoint="0.5,0">
        <GradientStop Color="{DynamicResource ControlLightColor}" Offset="0" />
        <GradientStop Color="{DynamicResource ControlMediumColor}" Offset="0.5" />
        <GradientStop Color="{DynamicResource ControlLightColor}" Offset="1" />
    </LinearGradientBrush>

    <!-- TopLevelHeader -->
    <ControlTemplate x:Key="{x:Static MenuItem.TopLevelHeaderTemplateKey}" TargetType="{x:Type MenuItem}">
        <Border x:Name="Border">
            <Grid>
                <ContentPresenter Margin="6,3,6,3" ContentSource="Header" RecognizesAccessKey="True" />
                <Popup x:Name="Popup" Placement="Bottom" IsOpen="{TemplateBinding IsSubmenuOpen}"
                         AllowsTransparency="True" Focusable="False" PopupAnimation="Fade">
                    <Border x:Name="SubmenuBorder" SnapsToDevicePixels="True" BorderThickness="1"
                            Background="{DynamicResource MenuPopupBrush}">
                        <Border.BorderBrush>
                            <SolidColorBrush Color="{DynamicResource BorderMediumColor}" />
                        </Border.BorderBrush>
                        <ScrollViewer CanContentScroll="True" Style="{StaticResource MenuScrollViewer}">
                            <StackPanel IsItemsHost="True" KeyboardNavigation.DirectionalNavigation="Cycle" />
                        </ScrollViewer>
                    </Border>
                </Popup>
            </Grid>
        </Border>
        <ControlTemplate.Triggers>
            <Trigger Property="IsSuspendingPopupAnimation" Value="true">
                <Setter TargetName="Popup" Property="PopupAnimation" Value="None" />
            </Trigger>
            <Trigger Property="IsHighlighted" Value="true">
                <Setter TargetName="Border" Property="BorderBrush" Value="Transparent" />
                <Setter Property="Background" TargetName="Border">
                    <Setter.Value>
                        <LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
                            <LinearGradientBrush.GradientStops>
                                <GradientStopCollection>
                                    <GradientStop Color="{StaticResource ControlLightColor}" />
                                    <GradientStop Color="{StaticResource ControlMouseOverColor}" Offset="1.0" />
                                </GradientStopCollection>
                            </LinearGradientBrush.GradientStops>
                        </LinearGradientBrush>
                    </Setter.Value>
                </Setter>
            </Trigger>
            <Trigger SourceName="Popup" Property="AllowsTransparency" Value="True">
                <Setter TargetName="SubmenuBorder" Property="CornerRadius" Value="0,0,4,4" />
                <Setter TargetName="SubmenuBorder" Property="Padding" Value="0,0,0,3" />
            </Trigger>
            <Trigger Property="IsEnabled" Value="False">
                <Setter Property="Foreground">
                    <Setter.Value>
                        <SolidColorBrush Color="{StaticResource DisabledForegroundColor}" />
                    </Setter.Value>
                </Setter>
            </Trigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>

    <!-- TopLevelItem -->
    <ControlTemplate x:Key="{x:Static MenuItem.TopLevelItemTemplateKey}" TargetType="{x:Type MenuItem}">
        <Border x:Name="Border">
            <Grid>
                <ContentPresenter Margin="6,3,6,3" ContentSource="Header" RecognizesAccessKey="True" />
            </Grid>
        </Border>
        <ControlTemplate.Triggers>
            <Trigger Property="IsHighlighted" Value="true">
                <Setter Property="Background" TargetName="Border">
                    <Setter.Value>
                        <LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
                            <LinearGradientBrush.GradientStops>
                                <GradientStopCollection>
                                    <GradientStop Color="{StaticResource ControlLightColor}" />
                                    <GradientStop Color="{StaticResource ControlMouseOverColor}" Offset="1.0" />
                                </GradientStopCollection>
                            </LinearGradientBrush.GradientStops>
                        </LinearGradientBrush>
                    </Setter.Value>
                </Setter>
            </Trigger>
            <Trigger Property="IsEnabled" Value="False">
                <Setter Property="Foreground">
                    <Setter.Value>
                        <SolidColorBrush Color="{StaticResource DisabledForegroundColor}" />
                    </Setter.Value>
                </Setter>
            </Trigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>

    <!-- SubmenuItem -->
    <ControlTemplate x:Key="{x:Static MenuItem.SubmenuItemTemplateKey}" TargetType="{x:Type MenuItem}">
        <Border x:Name="Border" BorderThickness="1">
            <Grid>
                <ContentPresenter x:Name="HeaderHost" ContentSource="Header" RecognizesAccessKey="True" />
            </Grid>
        </Border>
        <ControlTemplate.Triggers>
            <Trigger Property="IsHighlighted" Value="true">
                <Setter Property="Background" TargetName="Border">
                    <Setter.Value>
                        <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                            <GradientStop Color="Transparent" Offset="0" />
                            <GradientStop Color="{DynamicResource ControlMouseOverColor}" Offset="1" />
                        </LinearGradientBrush>
                    </Setter.Value>
                </Setter>
                <Setter Property="BorderBrush" TargetName="Border">
                    <Setter.Value>
                        <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                            <GradientStop Color="{DynamicResource BorderMediumColor}" Offset="0" />
                            <GradientStop Color="Transparent" Offset="1" />
                        </LinearGradientBrush>
                    </Setter.Value>
                </Setter>
            </Trigger>
            <Trigger Property="IsEnabled" Value="false">
                <Setter Property="Foreground">
                    <Setter.Value>
                        <SolidColorBrush Color="{StaticResource DisabledForegroundColor}" />
                    </Setter.Value>
                </Setter>
            </Trigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>

    <ControlTemplate x:Key="{x:Static MenuItem.SubmenuHeaderTemplateKey}"
             TargetType="{x:Type MenuItem}">
        <Border x:Name="Border" BorderThickness="1">
            <Grid>
                <ContentPresenter x:Name="HeaderHost" ContentSource="Header" RecognizesAccessKey="True" />
                <Popup x:Name="Popup" Placement="Right" HorizontalOffset="-4" IsOpen="{TemplateBinding IsSubmenuOpen}"
                        AllowsTransparency="True" Focusable="False" PopupAnimation="Fade">
                    <Border x:Name="SubmenuBorder" SnapsToDevicePixels="True" Background="{DynamicResource MenuPopupBrush}"
                            BorderThickness="1">
                        <Border.BorderBrush>
                            <SolidColorBrush Color="{DynamicResource BorderMediumColor}" />
                        </Border.BorderBrush>
                        <ScrollViewer CanContentScroll="True" Style="{StaticResource MenuScrollViewer}">
                            <StackPanel IsItemsHost="True" KeyboardNavigation.DirectionalNavigation="Cycle" />
                        </ScrollViewer>
                    </Border>
                </Popup>
            </Grid>
        </Border>
        <ControlTemplate.Triggers>
            <Trigger Property="IsHighlighted" Value="true">
                <Setter Property="Background" TargetName="Border">
                    <Setter.Value>
                        <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                            <GradientStop Color="Transparent" Offset="0" />
                            <GradientStop Color="{DynamicResource ControlMouseOverColor}" Offset="1" />
                        </LinearGradientBrush>
                    </Setter.Value>
                </Setter>
                <Setter Property="BorderBrush" TargetName="Border">
                    <Setter.Value>
                        <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                            <GradientStop Color="{DynamicResource BorderMediumColor}" Offset="0" />
                            <GradientStop Color="Transparent" Offset="1" />
                        </LinearGradientBrush>
                    </Setter.Value>
                </Setter>
            </Trigger>
            <Trigger SourceName="Popup" Property="AllowsTransparency" Value="True">
                <Setter TargetName="SubmenuBorder" Property="CornerRadius" Value="4" />
                <Setter TargetName="SubmenuBorder" Property="Padding" Value="0,3,0,3" />
            </Trigger>
            <Trigger Property="IsEnabled" Value="false">
                <Setter Property="Foreground">
                    <Setter.Value>
                        <SolidColorBrush Color="{StaticResource DisabledForegroundColor}" />
                    </Setter.Value>
                </Setter>
            </Trigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>

    <!-- MenuItem Style -->
    <Style x:Key="{x:Type MenuItem}"
   TargetType="{x:Type MenuItem}">
        <Setter Property="OverridesDefaultStyle"
      Value="True" />
        <Style.Triggers>
            <Trigger Property="Role"
         Value="TopLevelHeader">
                <Setter Property="Template"
          Value="{StaticResource {x:Static MenuItem.TopLevelHeaderTemplateKey}}" />
                <Setter Property="Grid.IsSharedSizeScope"
          Value="true" />
            </Trigger>
            <Trigger Property="Role"
         Value="TopLevelItem">
                <Setter Property="Template"
          Value="{StaticResource {x:Static MenuItem.TopLevelItemTemplateKey}}" />
            </Trigger>
            <Trigger Property="Role"
         Value="SubmenuHeader">
                <Setter Property="Template"
          Value="{StaticResource {x:Static MenuItem.SubmenuHeaderTemplateKey}}" />
            </Trigger>
            <Trigger Property="Role"
         Value="SubmenuItem">
                <Setter Property="Template"
          Value="{StaticResource {x:Static MenuItem.SubmenuItemTemplateKey}}" />
            </Trigger>
        </Style.Triggers>
    </Style>



</Window.Resources>
    <Grid Background="Red">
    <Grid.ContextMenu>
        <ContextMenu>
            <MenuItem Header="This is a menu item without InputGesture area"/>
        </ContextMenu>
    </Grid.ContextMenu>
   </Grid>

</Window>