如何更改菜单项样式

时间:2016-04-07 05:46:18

标签: c# wpf xaml

这是分级菜单。我想改变顶级和子菜单项的样式。也许这很容易,但我没有找到任何东西。

enter image description here

我的XAML:

<Menu Name="MainMenu" Background="White" ItemsSource="{Binding}" VerticalAlignment="Center">
   <Menu.ItemTemplate>
      <HierarchicalDataTemplate ItemsSource="{Binding Path=Objects1}">
         <StackPanel Orientation="Horizontal">
            <fa:ImageAwesome Icon="{Binding ObjectClass}" Width="{Binding ObjectSize}" Foreground="#616161" />
               <Label Content="{Binding ObjectName}" FontSize="14" HorizontalAlignment="Center" Foreground="#616161" FontFamily="Pe-icon-7-stroke"></Label>
         </StackPanel>
         <HierarchicalDataTemplate.ItemTemplate>
            <DataTemplate>
               <Label Content="{Binding ObjectName}" FontSize="14" HorizontalAlignment="Center" Foreground="#616161" FontFamily="Pe-icon-7-stroke"> 
               </Label>
            </DataTemplate>
         </HierarchicalDataTemplate.ItemTemplate>
     </HierarchicalDataTemplate>
  </Menu.ItemTemplate>
</Menu>

1 个答案:

答案 0 :(得分:1)

您可以授予Menu.ItemContainerStyle样式以设置菜单项上的任何属性,并类似HierarchicalDataTemplate.ItemContainerStyle以设置子菜单项上的任何样式。

 <Menu Name="MainMenu" Background="White" ItemsSource="{Binding}" VerticalAlignment="Center">
        <Menu.ItemContainerStyle>
            <Style TargetType="MenuItem">
                <Setter Property="Background" Value="Red"></Setter>
            </Style>
        </Menu.ItemContainerStyle>
        <Menu.ItemTemplate>
            <HierarchicalDataTemplate ItemsSource="{Binding Path=Objects1}">
                <StackPanel Orientation="Horizontal">
                    <Label Content="{Binding ObjectName}" FontSize="14" HorizontalAlignment="Center" Foreground="#616161" FontFamily="Pe-icon-7-stroke"></Label>
                </StackPanel>
                <HierarchicalDataTemplate.ItemTemplate>
                    <DataTemplate>
                        <Label Content="{Binding ObjectName}" FontSize="14" HorizontalAlignment="Center" Foreground="#616161" FontFamily="Pe-icon-7-stroke">
                        </Label>
                    </DataTemplate>
                </HierarchicalDataTemplate.ItemTemplate>
                <HierarchicalDataTemplate.ItemContainerStyle>
                    <Style TargetType="MenuItem">
                        <Setter Property="Background" Value="Blue"></Setter>
                    </Style>
                </HierarchicalDataTemplate.ItemContainerStyle>
            </HierarchicalDataTemplate>
        </Menu.ItemTemplate>
    </Menu>