缺少xaml中的菜单项

时间:2018-05-04 04:12:10

标签: .net wpf xaml

我最近创建了一个包含各种标题和子标题的菜单。然后在blend 2017中更改默认模板(自定义)。 之后,主脑可见,但副头缺失。 你可以看到下面的代码。

在此之前,它可以正常工作。

我的XAML代码:

  <MenuItem Header="Options" Foreground="White">
            <MenuItem Header="Volume" Template="{DynamicResource MenuItemControlTemplate1}">
                <MenuItem Header="Full" Template="{DynamicResource MenuItemControlTemplate1}"/>
                <MenuItem Header="Half" Template="{DynamicResource MenuItemControlTemplate1}"/>
                <MenuItem Header="Mute" Template="{DynamicResource MenuItemControlTemplate1}"/>
                <MenuItem Header="Increase By" Template="{DynamicResource MenuItemControlTemplate1}"/>
                <MenuItem Header="Increase To" Template="{DynamicResource MenuItemControlTemplate1}"/>
            </MenuItem>
            <Separator Style="{DynamicResource SeparatorStyle1}"/>
            <MenuItem Header="Playlist" Template="{DynamicResource MenuItemControlTemplate1}"/>
            <Separator Style="{DynamicResource SeparatorStyle1}"/>
            <MenuItem Header="Repeat" Template="{DynamicResource MenuItemControlTemplate1}"/>
            <MenuItem Header="Repeat All" Template="{DynamicResource MenuItemControlTemplate1}"/>
            <MenuItem Header="Shuffle" Template="{DynamicResource MenuItemControlTemplate1}"/>
            <Separator Style="{DynamicResource SeparatorStyle1}"/>
            <MenuItem Header="Play by Duratiuon" Template="{DynamicResource MenuItemControlTemplate1}">
                <MenuItem Header="Are less than 5 minutes" Template="{DynamicResource MenuItemControlTemplate1}"/>
                <MenuItem Header="Are less than 10 minutes" Template="{DynamicResource MenuItemControlTemplate1}"/>
                <MenuItem Header="Are less than 15 minutes" Template="{DynamicResource MenuItemControlTemplate1}"/>
                <MenuItem Header="Are less than 30 minutes" Template="{DynamicResource MenuItemControlTemplate1}"/>
                <MenuItem Header="Are less than 1 hour" Template="{DynamicResource MenuItemControlTemplate1}"/>
            </MenuItem>
            <MenuItem Header="Play Videos only" Template="{DynamicResource MenuItemControlTemplate1}"/>
            <MenuItem Header="Play Audios only" Template="{DynamicResource MenuItemControlTemplate1}"/>
            <MenuItem Header="Play Non-Stop with list" Template="{DynamicResource MenuItemControlTemplate1}"/>
            <MenuItem Header="Play Non-Stop without list" Template="{DynamicResource MenuItemControlTemplate1}"/>
            <MenuItem Header="Play even indexes" Template="{DynamicResource MenuItemControlTemplate1}"/>
            <MenuItem Header="Play odd indexes" Template="{DynamicResource MenuItemControlTemplate1}"/>
        </MenuItem>

看起来像这样: Selection type screen-shot.

选择时间图片this answer

我的菜单模板(由混合自动创建,我只是更改背景和选择颜色):

   <ControlTemplate x:Key="MenuItemControlTemplate1" TargetType="{x:Type MenuItem}">
        <Border x:Name="templateRoot" BorderBrush="Black"  BorderThickness="0" Background="Black"  SnapsToDevicePixels="True">
            <Grid Margin="0">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition MinWidth="22" SharedSizeGroup="MenuItemIconColumnGroup" Width="Auto"/>
                    <ColumnDefinition Width="13"/>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="30"/>
                    <ColumnDefinition SharedSizeGroup="MenuItemIGTColumnGroup" Width="Auto"/>
                    <ColumnDefinition Width="20"/>
                </Grid.ColumnDefinitions>
                <ContentPresenter x:Name="Icon" Content="{TemplateBinding Icon}" ContentSource="Icon" HorizontalAlignment="Center" Height="16" Margin="3" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="Center" Width="16"/>
                <Border x:Name="GlyphPanel" BorderBrush="Black"  BorderThickness="1" Background="Black"  ClipToBounds="False" HorizontalAlignment="Center" Height="22" Margin="-1,0,0,0" Visibility="Hidden"     VerticalAlignment="Center" Width="22">
                    <Path x:Name="Glyph" Data="F1M10,1.2L4.7,9.1 4.5,9.1 0,5.2 1.3,3.5 4.3,6.1 8.3,0 10,1.2z" Fill="RoyalBlue"  FlowDirection="LeftToRight" Height="11" Width="10"/>
                </Border>
                <ContentPresenter x:Name="menuHeaderContainer" ContentTemplate="{TemplateBinding HeaderTemplate}" Content="{TemplateBinding Header}" Grid.Column="2" ContentStringFormat="{TemplateBinding HeaderStringFormat}" ContentSource="Header" HorizontalAlignment="Left" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="Center"/>
                <TextBlock x:Name="menuGestureText" Grid.Column="4" Margin="{TemplateBinding Padding}" Opacity="0.7" Text="{TemplateBinding InputGestureText}" VerticalAlignment="Center"/>
            </Grid>
        </Border>
        <ControlTemplate.Triggers>
            <Trigger Property="Icon" Value="{x:Null}">
                <Setter Property="Visibility" TargetName="Icon" Value="Collapsed"/>
            </Trigger>
            <Trigger Property="IsChecked" Value="True">
                <Setter Property="Visibility" TargetName="GlyphPanel" Value="Visible"/>
                <Setter Property="Visibility" TargetName="Icon" Value="Collapsed"/>
            </Trigger>
            <Trigger Property="IsHighlighted" Value="True">
                <Setter Property="Background" TargetName="templateRoot" Value="#FF6027CB"/>
                <Setter Property="BorderBrush" TargetName="templateRoot" Value="#FF6027CB"/>
            </Trigger>
            <Trigger Property="IsEnabled" Value="False">
                <Setter Property="TextElement.Foreground" TargetName="templateRoot" Value="#FF707070"/>
                <Setter Property="Fill" TargetName="Glyph" Value="#FF707070"/>
            </Trigger>
            <MultiTrigger>
                <MultiTrigger.Conditions>
                    <Condition Property="IsHighlighted" Value="True"/>
                    <Condition Property="IsEnabled" Value="False"/>
                </MultiTrigger.Conditions>
                <Setter Property="Background" TargetName="templateRoot" Value="#0A000000"/>
                <Setter Property="BorderBrush" TargetName="templateRoot" Value="#21000000"/>
            </MultiTrigger>
        </ControlTemplate.Triggers>
    </ControlTemplate> 

下面是我的分隔符模板(您可以看到图像中的绿色分隔符,但这没有任何影响。应用上部菜单模板后子标题变得不可见,但您也可以检查此标题)

我的分隔符模板:

  <SolidColorBrush x:Key="Menu.Static.Separator" Color="Green"/>
    <Style x:Key="SeparatorStyle1" TargetType="{x:Type Separator}">
        <Setter Property="Background" Value="{StaticResource Menu.Static.Separator}"/>
        <Setter Property="BorderBrush" Value="{StaticResource Menu.Static.Separator}"/>
        <Setter Property="MinHeight" Value="1"/>
        <Setter Property="SnapsToDevicePixels" Value="true"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Separator}">
                    <Border BorderBrush="Green"  BorderThickness="1" Background="Green" Margin="30,0,0,0"/>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Window.Resources> 

它有什么问题,任何类型的帮助都将受到赞赏。

由于

ABHI

1 个答案:

答案 0 :(得分:1)

您的自定义ControlTemplate是此处的问题。为ControlTemplate创建自定义MenuItem比使用Blend复制默认模板更多。

您还需要定义TopLevelHeader模板以及TopLevelItemSubmenuItem模板。有关此内容的更多信息,请参阅官方文档。

菜单和MenuItem ControlTemplate示例: https://docs.microsoft.com/en-us/dotnet/framework/wpf/controls/menu-styles-and-templates

如果要复制默认模板,可以使用反编译器(如dotPeek)并反编译PresentationFramework.Aero2程序集。