Как использовать MenuItem с дочерними MenuItems, когда он имеет собственный ControlTemplate и определен в отдельном файле XAML? - PullRequest
0 голосов
/ 14 июня 2019

У меня есть MenuItem внутри меню, который иногда должен показывать промежуток времени в правой части (промежуток времени, который уменьшается секунда на секунда). Чтобы внести это изменение, я создал новый XAML-файл подкласса MenuItem. В нем я устанавливаю свойство Template элемента MenuItem на шаблон, который автоматически создается Visual Studio, когда я нажимаю «Изменить шаблон»> «Редактировать копию ...» в контекстном меню пункта меню в конструкторе. Когда этот шаблон установлен, подэлементы, заданные непосредственно в свойстве MenuItem.Items внутри нового файла XAML, не отображаются, как если бы к нему не было добавлено ни одного дочернего элемента меню. Я хочу использовать шаблон, определенный в XAML, а также иметь дочерние элементы одновременно.

screenshot

Я пытался не помещать дочерние элементы меню в новый файл XAML, а непосредственно в MainWindow.xaml, но я достиг этого ограничения .

Я также гуглил «шаблон меню wpf с подпунктами», но безуспешно.

В MainWindow.xaml:

<MenuItem Header="_Window">
    <local:KeepOnTopMenuItem Header="_Keep on Top..." x:Name="MiKeepOnTop">
    </local:KeepOnTopMenuItem>
</MenuItem>

и вся разметка в KeepOnTopMenuItem.xaml:

<MenuItem x:Class="cs_timed_silver.KeepOnTopMenuItem"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:cs_timed_silver"
             mc:Ignorable="d" 
             d:DesignHeight="50" d:DesignWidth="200" Template="{DynamicResource MenuItemControlTemplate1}">
    <MenuItem.Resources>
        <ControlTemplate x:Key="MenuItemControlTemplate1" TargetType="{x:Type MenuItem}">
            <Border x:Name="templateRoot" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
                <Grid Margin="-1">
                    <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"/>
                        <ColumnDefinition Width="Auto"/>
                    </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="#FF26A0DA" BorderThickness="1" Background="#3D26A0DA" 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="#FF212121" 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"/>
                    <TextBlock x:Name="remainingTimeSpanText" Grid.Column="5" Margin="{TemplateBinding Padding}" VerticalAlignment="Center"
                               FontStyle="Italic" Foreground="gray"/>
                </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="#3D26A0DA"/>
                    <Setter Property="BorderBrush" TargetName="templateRoot" Value="#FF26A0DA"/>
                </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>
    </MenuItem.Resources>
    <MenuItem.Items>
        <MenuItem Command="local:CustomCommands.KeepOnTopAlways"
                CommandParameter="{Binding IsChecked, RelativeSource={RelativeSource Mode=Self}}"
                IsCheckable="True"
                x:Name="MiKeepOnTopAlways"/>
        <MenuItem Command="local:CustomCommands.KeepOnTopUntilNextRestart"
                CommandParameter="{Binding IsChecked, RelativeSource={RelativeSource Mode=Self}}"
                IsCheckable="True"
                Name="MiKeepOnTopUntilNextRestart"/>
        <MenuItem IsCheckable="True"
                Command="local:CustomCommands.KeepOnTopFor5Min"
                CommandParameter="{Binding IsChecked, RelativeSource={RelativeSource Mode=Self}}"
                x:Name="MiKeepOnTopFor5Min"/>
        <MenuItem IsCheckable="True"
                Command="local:CustomCommands.KeepOnTopFor15Min"
                CommandParameter="{Binding IsChecked, RelativeSource={RelativeSource Mode=Self}}"
                x:Name="MiKeepOnTopFor15Min"/>
        <MenuItem IsCheckable="True"
                Command="local:CustomCommands.KeepOnTopFor1Hour"
                CommandParameter="{Binding IsChecked, RelativeSource={RelativeSource Mode=Self}}"
                x:Name="MiKeepOnTopFor1Hour"/>
    </MenuItem.Items>

    <MenuItem.Icon>
        <Image Source="/Resources/Oxygen-Icons.org-Oxygen-Actions-go-top.ico"/>
    </MenuItem.Icon>
</MenuItem>

Ожидаемый результат состоит в том, что отображаются элементы подменю, а фактическим результатом является то, что KeepOnTopMenuItem отображается как без дочерних элементов.

Я не уверен, что делать дальше. Что я могу сделать, чтобы использовать пользовательский шаблон MenuItem вместе с дочерними элементами?

1 Ответ

1 голос
/ 14 июня 2019

Редактировать шаблон-> Редактировать копию не дает правильного шаблона.

Если вы используете декомпилятор, такой как dotPeek и откроете C:\Windows\Microsoft.NET\Framework64\v4.0.30319\WPF\PresentationFramework.Aero2.dll, вы найдетешаблоны по умолчанию в themes/aero2.normalcolor.baml.

Если вы посмотрите на шаблон MenuItem, вы увидите, что он применяет другое ControlTemplate в зависимости от значения свойства Role:

<Style.Triggers>
    <Trigger Property="MenuItem.Role" Value="TopLevelHeader">
        <Setter Property="Control.Template"
                Value="{DynamicResource {ComponentResourceKey TypeInTargetAssembly={x:Type MenuItem}, ResourceId=TopLevelHeaderTemplateKey}}"/>
        <Setter Property="Control.Padding" Value="6,0"/>
    </Trigger>
    <Trigger Property="MenuItem.Role" Value="TopLevelItem">
        <Setter Property="Control.Template"
                Value="{DynamicResource {ComponentResourceKey TypeInTargetAssembly={x:Type MenuItem}, ResourceId=TopLevelItemTemplateKey}}"/>
        <Setter Property="Control.Padding" Value="6,0"/>
    </Trigger>
    <Trigger Property="MenuItem.Role" Value="SubmenuHeader">
        <Setter Property="Control.Template"
                Value="{DynamicResource {ComponentResourceKey TypeInTargetAssembly={x:Type MenuItem}, ResourceId=SubmenuHeaderTemplateKey}}"/>
    </Trigger>
</Style.Triggers>

Это один из этих шаблонов, который вы должны настроить, чтобы не нарушать поведение по умолчанию.

...