0

我有一个用 MVVM 制作的菜单,但我的 Menuitem 很大,如下所示 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>

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

4

1 回答 1

0

我相信您看到的是默认模板为 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>
于 2012-06-12T21:04:51.317 回答