如何在WPF菜单根项目中显示图标?

时间:2019-06-13 20:53:42

标签: c# wpf menu icons

我正在设计需要显示菜单项的WPF应用程序。为了设计友好的用户布局,我想在每个菜单项中甚至在根元素中都显示一个图标。除菜单的根元素外,所有菜单项均正确显示图标...。有什么特殊的方法吗?

我尝试将MenuItem.Icon标记放在每个包含根元素的菜单项上,但是正如我所说,只有根项不能显示图标。我正在使用C#.Net Framework 4.7.2

<DockPanel>
    <Menu Name="MnuMain" DockPanel.Dock="Top">
        <MenuItem Header="Inicio">
            <MenuItem.Icon>
                <Image Source="D:\Trabajo\Componentes\Proyectos\.NET\Librerias\WpfSecurity\Imagenes\32x32\green_flag.png" Visibility="Visible" /> <!-- This icon doesn't show in my aplication -->
            </MenuItem.Icon>
            <MenuItem Header="LogIn" Click="MenuItem_Click" Name="MnuLogIn">
                <MenuItem.Icon>
                    <Image Source="D:\Trabajo\Componentes\Proyectos\.NET\Librerias\WpfSecurity\Imagenes\32x32\green_unlock.png" /> <!-- This icon shows correctly in my aplication -->
                </MenuItem.Icon>
            </MenuItem>

如何在所有菜单项上甚至在根元素中显示图标?

This is how my app show the menu... i want to add an icon to the "Inicio" element.

更新:我忘了提到我的菜单使用样式来设置一些可见性定义...这将是问题所在吗?...这是样式模板。

<Window.Resources>
    <!-- ************************* 
    * STYLE: MenuItem 
    ************************** -->
    <SolidColorBrush x:Key="HighlightedBackgroundBrush" Color="#FF797878" />
    <SolidColorBrush x:Key="MenuBackgroundBrush" Color="#FF505050" />
    <SolidColorBrush x:Key="NormalBorderBrush" Color="#FFFFFFFF" />
    <SolidColorBrush x:Key="SolidMenuFontBrush" Color="#FFFFFFFF" />
    <SolidColorBrush x:Key="HighlightedText" Color="#FFFFFFFF" />
    <Style x:Key="{x:Type Menu}" TargetType="{x:Type Menu}">
        <Setter Property="OverridesDefaultStyle" Value="True"/>
        <Setter Property="SnapsToDevicePixels" Value="True"/>
        <Setter Property="Height" Value="25"/>
        <Setter Property="Foreground" Value="{StaticResource SolidMenuFontBrush}"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Menu}">
                    <Border Background="{StaticResource MenuBackgroundBrush}" 
                            BorderBrush="{StaticResource MenuBackgroundBrush}" 
                            BorderThickness="1">
                        <StackPanel ClipToBounds="True" Orientation="Horizontal" IsItemsHost="True"/>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    <Style x:Key="{x:Type MenuItem}" TargetType="{x:Type MenuItem}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type MenuItem}">
                    <Border x:Name="Border" 
                        Background="{TemplateBinding Background}" 
                        BorderBrush="{TemplateBinding BorderBrush}" 
                        BorderThickness="1">
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition x:Name="Col0" MinWidth="17" Width="Auto" SharedSizeGroup="MenuItemIconColumnGroup"/>
                                <ColumnDefinition x:Name="Col1" Width="Auto" SharedSizeGroup="MenuTextColumnGroup"/>
                                <ColumnDefinition x:Name="Col2" Width="Auto" SharedSizeGroup="MenuItemIGTColumnGroup"/>
                                <ColumnDefinition x:Name="Col3" Width="14"/>
                            </Grid.ColumnDefinitions>

                            <!-- ContentPresenter to show an Icon if needed -->
                            <ContentPresenter Grid.Column="0" Margin="4,0,6,0" x:Name="Icon" VerticalAlignment="Center" HorizontalAlignment="Left" ContentSource="Icon" Width="20" Height="20"/>

                            <!-- Glyph is a checkmark if needed for a checkable menu -->
                            <Grid Grid.Column="0" Visibility="Hidden" Margin="4,0,6,0" x:Name="GlyphPanel" VerticalAlignment="Center">
                                <Path x:Name="GlyphPanelpath" VerticalAlignment="Center" Fill="{TemplateBinding Foreground}" Data="M0,2 L0,4.8 L2.5,7.4 L7.1,2.8 L7.1,0 L2.5,4.6 z" FlowDirection="LeftToRight"/>
                            </Grid>

                            <!-- Content for the menu text etc -->
                            <ContentPresenter Grid.Column="1" 
                               Margin="{TemplateBinding Padding}" 
                               x:Name="HeaderHost" 
                               RecognizesAccessKey="True" 
                               ContentSource="Header"/>

                            <!-- Content for the menu IGT -->
                            <ContentPresenter Grid.Column="2" 
                               Margin="8,1,8,1" 
                               x:Name="IGTHost" 
                               ContentSource="InputGestureText" 
                               VerticalAlignment="Center"/>

                            <!-- Arrow drawn path which points to the next level of the menu -->
                            <Grid Grid.Column="3" Margin="4,0,6,0" x:Name="ArrowPanel" VerticalAlignment="Center">
                                <Path x:Name="ArrowPanelPath" HorizontalAlignment="Right" VerticalAlignment="Center" Fill="{TemplateBinding Foreground}" Data="M0,0 L0,8 L4,4 z"/>
                            </Grid>

                            <!-- The Popup is the body of the menu which expands down or across depending on the level of the item -->
                            <Popup IsOpen="{Binding Path=IsSubmenuOpen, RelativeSource={RelativeSource TemplatedParent}}" Placement="Right" 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" Background="{DynamicResource MenuBackgroundBrush}">
                                        <!-- StackPanel holds children of the menu. This is set by IsItemsHost=True -->
                                        <StackPanel IsItemsHost="True" Background="{DynamicResource MenuBackgroundBrush}" KeyboardNavigation.DirectionalNavigation="Cycle"/>
                                    </Grid>
                                </Border>
                            </Popup>
                        </Grid>
                    </Border>

                    <!-- These triggers re-configure the four arrangements of MenuItem to show different levels of menu via Role -->
                    <ControlTemplate.Triggers>
                        <!-- Role = TopLevelHeader : this is the root menu item in a menu; the Popup expands down -->
                        <Trigger Property="Role" Value="TopLevelHeader">
                            <Setter Property="Padding" Value="6,1,6,1"/>
                            <Setter Property="Placement" Value="Bottom" TargetName="SubMenuPopup"/>
                            <Setter Property="MinWidth" Value="0" TargetName="Col0"/>
                            <Setter Property="Width" Value="Auto" TargetName="Col3"/>
                            <Setter Property="Visibility" Value="Collapsed" TargetName="Icon"/>
                            <Setter Property="Visibility" Value="Collapsed" TargetName="GlyphPanel"/>
                            <Setter Property="Visibility" Value="Collapsed" TargetName="IGTHost"/>
                            <Setter Property="Visibility" Value="Collapsed" TargetName="ArrowPanel"/>
                        </Trigger>

                        <!-- Role = TopLevelItem : this is a child menu item from the top level without any child items-->
                        <Trigger Property="Role" Value="TopLevelItem">
                            <Setter Property="Padding" Value="6,1,6,1"/>
                            <Setter Property="Visibility" Value="Collapsed" TargetName="ArrowPanel"/>
                        </Trigger>

                        <!-- Role = SubMenuHeader : this is a child menu item which does not have children -->
                        <Trigger Property="Role" Value="SubmenuHeader">
                            <Setter Property="DockPanel.Dock" Value="Top"/>
                            <Setter Property="Padding" Value="0,2,0,2"/>
                        </Trigger>

                        <!-- Role = SubMenuItem : this is a child menu item which has children-->
                        <Trigger Property="Role" Value="SubmenuItem">
                            <Setter Property="DockPanel.Dock" Value="Top"/>
                            <Setter Property="Padding" Value="0,2,0,2"/>
                            <Setter Property="Visibility" Value="Collapsed" TargetName="ArrowPanel"/>
                        </Trigger>
                        <Trigger Property="IsSuspendingPopupAnimation" Value="true">
                            <Setter Property="PopupAnimation" Value="None" TargetName="SubMenuPopup"/>
                        </Trigger>

                        <!-- If no Icon is present the we collapse the Icon Content -->
                        <Trigger Property="Icon" Value="{x:Null}">
                            <Setter Property="Visibility" Value="Collapsed" TargetName="Icon"/>
                        </Trigger>

                        <!-- The GlyphPanel contains the CheckMark -->
                        <Trigger Property="IsChecked" Value="true">
                            <Setter Property="Visibility" Value="Visible" TargetName="GlyphPanel"/>
                            <Setter Property="Visibility" Value="Collapsed" TargetName="Icon"/>
                        </Trigger>

                        <!-- Using the system colors for the Menu Highlight and IsEnabled-->
                        <Trigger Property="IsHighlighted" Value="true">
                            <Setter Property="Background" Value="{StaticResource HighlightedBackgroundBrush}" TargetName="Border"/>
                            <Setter Property="Foreground" Value="{StaticResource HighlightedText}"/>
                            <Setter Property="BorderBrush" Value="{StaticResource NormalBorderBrush}" TargetName="Border"/>
                        </Trigger>
                        <Trigger Property="IsHighlighted" Value="false">
                            <Setter Property="Background" Value="{StaticResource MenuBackgroundBrush}" TargetName="Border"/>
                            <Setter Property="Foreground" Value="{StaticResource SolidMenuFontBrush}"/>
                            <Setter Property="BorderBrush" Value="{StaticResource MenuBackgroundBrush}" TargetName="Border"/>
                        </Trigger>
                        <Trigger Property="IsEnabled" Value="false">
                            <Setter Property="Foreground" Value="LightGray"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Window.Resources>

3 个答案:

答案 0 :(得分:0)

尝试一下:

<MenuItem   Name="MainSettingsMenu">
<Image HorizontalAlignment="Left" Margin="0,-20,0,0" Width="20" Height="20" Source="Image/gtext.png"/>
</MenuItem>

答案 1 :(得分:0)

此代码可以正常工作,请重建应用程序或检查未显示的图像。

<Grid HorizontalAlignment="Center" VerticalAlignment="Center">
        <DockPanel>
            <Menu Name="MnuMain" DockPanel.Dock="Top">
                <MenuItem Header="Inicio">
                    <MenuItem.Icon>
                        <Image Source="Image/pr.png " />
                        </MenuItem.Icon>
                    <MenuItem Header="LogIn"  Name="MnuLogIn">
                        <MenuItem.Icon>
                            <Image Source="Image/sys.png" />
                            </MenuItem.Icon>
                    </MenuItem>
                </MenuItem>
            </Menu>
        </DockPanel>
    </Grid>

答案 2 :(得分:0)

要使根MenuItem的图标可见,请将此设置器设置为Visible或将其删除:

<ControlTemplate.Triggers>
    <!-- Role = TopLevelHeader : this is the root menu item in a menu; the Popup expands down -->
    <Trigger Property="Role" Value="TopLevelHeader">
        ...

        <!-- Remove this setter or set it to Visible -->
        <Setter TargetName="Icon" 
                Property="Visibility"
                Value="Visible" />
        ...
    </Trigger>
    ...
</ControlTemplate.Triggers>