菜单项不会下拉(WPF)

时间:2018-11-20 14:25:16

标签: c# wpf menu

我实际上遇到的问题是,通过单击菜单,子菜单项不会被下拉。 请您告诉我我做错了什么,并帮助我解决此问题吗?

这是xaml代码:

<Window.Resources>
    <Style TargetType="MenuItem" x:Key="myStyle">
        <Setter Property="Background" Value="#FFEAA40D"/>
        <Setter Property="Foreground" Value="Black"/>
        <Setter Property="FontSize" Value="10"/>
        <Setter Property="MinWidth" Value="150"/>
        <Setter Property="MinHeight" Value="50"/>
        <Setter Property="Margin" Value="15"/>
        <Setter Property="FontSize" Value="15"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type MenuItem}">
                    <Border x:Name="border" Background="{TemplateBinding Background}" BorderBrush="White" BorderThickness="2" CornerRadius="2">
                        <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" ContentSource="Header"/>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsPressed" Value="true">
                            <Setter Property="BorderBrush" TargetName="border" Value="#FFEAA40D"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Style.Triggers>
            <Trigger Property="IsMouseOver" Value="true">
                <Setter Property="Background" Value="#FFC3670B"></Setter>
                <Setter Property="Foreground" Value="White"></Setter>
            </Trigger>
            <Trigger Property="IsPressed" Value="true">
                <Setter Property="Background" Value="Black"></Setter>
                <Setter Property="Foreground" Value="#FFEAA40D"></Setter>
            </Trigger>
        </Style.Triggers>
    </Style>
</Window.Resources>

<DockPanel>
    <Menu DockPanel.Dock="Top">
        <MenuItem Header="File" Style="{StaticResource myStyle}">
            <MenuItem Header="Close" />
        </MenuItem>
        <MenuItem Header="Tools" Style="{StaticResource myStyle}">
            <MenuItem Header="Create"></MenuItem>
            <MenuItem Header="Edit"></MenuItem>
        </MenuItem>
        <MenuItem Header="Help" Style="{StaticResource myStyle}">
        </MenuItem>
    </Menu>
    <Grid/>
</DockPanel>

在此先感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

ControlTemplate中,您尚未添加Popup,这就是为什么您的子菜单项未显示的原因。我已更改为您的 XAML ;请参阅下面的内容。

<Setter.Value>
    <ControlTemplate TargetType="{x:Type MenuItem}">
        <Grid>
            <Border x:Name="border" Background="{TemplateBinding Background}" BorderBrush="White" BorderThickness="2" CornerRadius="2">
                <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" ContentSource="Header"/>
            </Border>
            <Popup IsOpen="{Binding Path=IsSubmenuOpen, RelativeSource={RelativeSource TemplatedParent}}" Placement="Bottom" x:Name="SubMenuPopup" 
                   Focusable="false" PopupAnimation="{DynamicResource {x:Static SystemParameters.MenuPopupAnimationKey}}">
                <Border x:Name="SubMenuBorder" BorderBrush="{Binding Path=Foreground, RelativeSource={RelativeSource AncestorType={x:Type Menu}}}" 
                        BorderThickness="1" Padding="2,2,2,2">
                    <Grid x:Name="SubMenu" Grid.IsSharedSizeScope="True">                       
                        <StackPanel IsItemsHost="True" KeyboardNavigation.DirectionalNavigation="Cycle" Background="#FFEAA40D" />
                    </Grid>
                </Border>
            </Popup>
        </Grid>
        <ControlTemplate.Triggers>
            <Trigger Property="IsPressed" Value="true">
                <Setter Property="BorderBrush" TargetName="border" Value="#FFEAA40D"/>
            </Trigger>
        </ControlTemplate.Triggers>         
    </ControlTemplate>
</Setter.Value>