0

我有一个ContextMenu由许多顶级MenuItems组成的,其中一个包含一个项目的子菜单,受ItemsSource以下约束:

<ContextMenu Style="{x:Null}">
    <MenuItem Header="{Binding MenuLabelNewSolution}" Command="New"/>
    <MenuItem Header="{Binding MenuLabelOpenSolution}" Command="Open"/>
    <MenuItem Header="{Binding MenuLabelRecentSolutions}"
              ItemsSource="{Binding RecentSolutions, Mode=OneWay}">
        <MenuItem.ItemTemplate>
            <DataTemplate>
                <Button Style="{x:Null}" Margin="0" Content="Test"
                        Command="vm:CustomCommands.ExplicitOpen"/>
            </DataTemplate>
        </MenuItem.ItemTemplate>
    </MenuItem>
    <MenuItem Header="{Binding MenuLabelSaveAll}" Command="vm:CustomCommands.SaveAll"/>
</ContextMenu>

上面的子菜单项是Button用来说明问题的测试。

下面,我缩减了Style扩展MenuItem列以说明问题:

<Style TargetType="{x:Type MenuItem}">
    <Setter Property="Background" Value="Transparent" />
    <Setter Property="Foreground" Value="Black" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type MenuItem}">
                <Border Style="{x:Null}" Background="LightBlue"
                        BorderBrush="Black" BorderThickness="1">
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="100" />
                            <ColumnDefinition Width="*" />
                            <ColumnDefinition Width="100" />
                        </Grid.ColumnDefinitions>
                        <ContentPresenter Grid.Column="1" ContentSource="Header" />
                        <Popup Style="{x:Null}" Margin="0" 
                               IsOpen="{Binding Path=IsSubmenuOpen, RelativeSource={RelativeSource TemplatedParent}}"
                               Placement="Right" VerticalOffset="-3">
                            <StackPanel Style="{x:Null}" Background="Red"
                                        Margin="0" IsItemsHost="True" />
                        </Popup>
                    </Grid>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

我遇到的问题是子菜单有很多非活动死区,如下图所示:

死区菜单

如果用户点击顶层的死区MenuItem(用于图标和手势文本),则执行菜单命令。但是,如果用户单击子菜单项中测试按钮之外的任何死区,则不会执行菜单命令。

我该怎么做Style才能摆脱子菜单级别的额外空间,或者至少使整个子菜单区域相对于菜单命令处于活动状态?

顺便说一句,如果我恢复为默认值StyleMenuItem我仍然会遇到同样的问题:

默认菜单

4

1 回答 1

0

问题根本不MenuItem Style在于我,而是因为我将 aDataTemplate用于子菜单项。以下更改起到了作用:

<ContextMenu>
    <MenuItem Header="{Binding MenuLabelNewSolution}" Command="New"/>
    <MenuItem Header="{Binding MenuLabelOpenSolution}" Command="Open"/>
    <MenuItem Header="{Binding MenuLabelRecentSolutions}"
              ItemsSource="{Binding RecentSolutions, Mode=OneWay}">
        <MenuItem.ItemContainerStyle>
            <Style TargetType="MenuItem">
                <Setter Property="Header" Value="{Binding Header}"/>
                <Setter Property="Command" Value="vm:CustomCommands.ExplicitOpen"/>
            </Style>
        </MenuItem.ItemContainerStyle>
    </MenuItem>
    <MenuItem Header="{Binding MenuLabelSaveAll}" Command="vm:CustomCommands.SaveAll"/>
</ContextMenu>
于 2013-03-19T04:43:21.160 回答