在顶级菜单项上使用MenuItem.SubmenuItemTemplateKey

时间:2015-04-03 13:11:21

标签: wpf menuitem

顶级MenuItem UIElements的样式似乎与较低级别的MenuItem不同。我想在顶级使用与子级别相同的样式。如何将顶级样式替换为子级别的样式?

我看到两种风格都有一个资源键... MenuItem.SubmenuItemTemplateKey和 MenuItem.TopLevelItemTemplateKey

1 个答案:

答案 0 :(得分:1)

为menuitem的targettype创建一个Controltemplate并将其用于:

<ControlTemplate x:Key="MyMenuItemTemplateKey" TargetType="MenuItem">
    <Grid Name="Border" Height="25">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="32" SharedSizeGroup="Icon"/>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="Auto" SharedSizeGroup="Shortcut"/>
            <ColumnDefinition Width="13"/>
        </Grid.ColumnDefinitions>
        <Border Name="BorderIcon" Grid.Column="0" BorderBrush="Red" BorderThickness="0,0,1,0">
            <ContentPresenter Name="Icon" Margin="5,0,5,0" VerticalAlignment="Center" ContentSource="Icon"/>
        </Border>
        <ContentPresenter Name="HeaderHost" Grid.Column="1" ContentSource="Header" Margin="5,0,0,0" VerticalAlignment="Center" RecognizesAccessKey="True"/>
        <TextBlock x:Name="InputGestureText" Grid.Column="2" Text="{TemplateBinding InputGestureText}" Margin="10,2,2,2" VerticalAlignment="Center" DockPanel.Dock="Right" />
        <Border Name="BorderChecked" Grid.Column="3" CornerRadius="2" Background="Transparent" Margin="5,2,5,2"/>
        <Border Name="BorderDisable" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Background="Transparent"/>
    </Grid>
    <ControlTemplate.Triggers>
        <Trigger Property="Icon" Value="{x:Null}">
            <Setter TargetName="Icon" Property="Visibility" Value="Hidden"/>
        </Trigger>
        <Trigger Property="IsHighlighted" Value="true">
            <Setter TargetName="Border" Property="Background" Value="Blue"/>
        </Trigger>
        <Trigger Property="IsChecked" Value="True">
            <Setter TargetName="BorderChecked" Property="Background" Value="Green"/>
        </Trigger>
        <Trigger Property="IsEnabled" Value="False">
            <Setter TargetName="BorderDisable" Property="Background" Value="#3FFFFFFF"/>
        </Trigger>
        <Trigger Property="IsEnabled" Value="True">
            <Setter Property="Foreground" Value="Black"/>
        </Trigger>
    </ControlTemplate.Triggers>
</ControlTemplate>

制作一个样式,用同一个模板键定义两个模板:

<Style x:Key="MyMenuItemStyle" TargetType="MenuItem">
    <Setter Property="OverridesDefaultStyle" Value="True"/>
    <Setter Property="FontFamily" Value="Verdana"/>
    <Setter Property="FontSize" Value="8.5pt"/>
    <Style.Triggers>
        <Trigger Property="Role" Value="TopLevelItem">
            <Setter Property="Template" Value="{StaticResource MyMenuItemTemplateKey}"/>
        </Trigger>

        <Trigger Property="Role" Value="SubmenuItem">
            <Setter Property="Template" Value="{StaticResource MyMenuItemTemplateKey}"/>
        </Trigger>
        <Trigger Property="IsChecked" Value="True">
            <Setter Property="Foreground" Value="Green"/>
        </Trigger>
    </Style.Triggers>
</Style>