WPF:MenuItem样式

基础信息

1、MenuItem 样式

    <Window.Resources>

        <Style TargetType="{x:Type MenuItem}">
            <!--<Setter Property="Background" Value="#999999" />-->
            <!--<Setter Property="Foreground" Value="#999999" />-->
            <!--MenuItem文字颜色和menu的文字颜色一样-->
            <!--<Setter Property="Foreground" Value="{Binding Path=Foreground, RelativeSource={RelativeSource AncestorType={x:Type Menu}}}"/>-->
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type MenuItem}">
                        <Border x:Name="Border"
                                Background="{TemplateBinding Background}"
                                BorderBrush="{TemplateBinding BorderBrush}"
                                BorderThickness="{TemplateBinding BorderThickness}">
                            <Grid>
                                <Grid.ColumnDefinitions>
                                    <!--<ColumnDefinition x:Name="Col0" SharedSizeGroup="MenuItemIconColumnGroup" MinWidth="17" Width="Auto"/>-->
                                    <ColumnDefinition Width="Auto"  SharedSizeGroup="MenuTextColumnGroup"/>
                                    <!--<ColumnDefinition 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" ContentSource="Icon"/>

                                <!-- 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"
                                          VerticalAlignment="Center"
                                          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">
                                            <!-- StackPanel holds children of the menu. This is set by IsItemsHost=True  KeyboardNavigationMode = "Cycle" -->
                                            <StackPanel IsItemsHost="True" 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="Left" TargetName="SubMenuPopup"/>
                                <!--<Setter Property="MinWidth" Value="0" TargetName="Col0"/>-->
                                <!--<Setter Property="Width" Value="Auto" TargetName="Col3"/>-->
                                <Setter Property="CornerRadius" Value="5,5,0,0" TargetName="Border"/>
                                <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="#ff0000" TargetName="Border"/>
                                <Setter Property="Foreground" Value="#0036ff"/>
                            </Trigger>
                            <Trigger Property="IsEnabled" Value="false">
                                <Setter Property="Foreground" Value="#ff0000"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

        <!--工具数据源-->
        <XmlDataProvider x:Key="toolsDS" Source="Config\Tools\Tools.xml" XPath="Tools/Tool"></XmlDataProvider>
        <!--Tool模板-->
        <HierarchicalDataTemplate DataType="Tool" ItemsSource="{Binding XPath=Tool}" >
            <StackPanel Orientation="Horizontal">
                <TextBlock Tag ="{Binding [email protected]}" Width="38">
                    <Image Source="{Binding [email protected]}" Width="38" Height="38"></Image>
                </TextBlock>
            </StackPanel>
        </HierarchicalDataTemplate>

    </Window.Resources>

2、Menu、MenuItem、 直接拷贝就可查看示例

    <Window.Resources>

        <!--Control colors.-->
        <Color x:Key="WindowColor">#FFE8EDF9</Color>
        <Color x:Key="ContentAreaColorLight">#FFC5CBF9</Color>
        <Color x:Key="ContentAreaColorDark">#FF7381F9</Color>

        <Color x:Key="DisabledControlLightColor">#FFE8EDF9</Color>
        <Color x:Key="DisabledControlDarkColor">#FFC5CBF9</Color>
        <Color x:Key="DisabledForegroundColor">#FF888888</Color>

        <Color x:Key="SelectedBackgroundColor">#FFC5CBF9</Color>
        <Color x:Key="SelectedUnfocusedColor">#FFDDDDDD</Color>

        <Color x:Key="ControlLightColor">White</Color>
        <Color x:Key="ControlMediumColor">#FF7381F9</Color>
        <Color x:Key="ControlDarkColor">#FF211AA9</Color>

        <Color x:Key="ControlMouseOverColor">#FF3843C4</Color>
        <Color x:Key="ControlPressedColor">#FF211AA9</Color>

        <Color x:Key="GlyphColor">#FF444444</Color>
        <Color x:Key="GlyphMouseOver">sc#1, 0.004391443, 0.002428215, 0.242281124</Color>

        <!--Border colors-->
        <Color x:Key="BorderLightColor">#FFCCCCCC</Color>
        <Color x:Key="BorderMediumColor">#FF888888</Color>
        <Color x:Key="BorderDarkColor">#FF444444</Color>

        <Color x:Key="PressedBorderLightColor">#FF888888</Color>
        <Color x:Key="PressedBorderDarkColor">#FF444444</Color>

        <Color x:Key="DisabledBorderLightColor">#FFAAAAAA</Color>
        <Color x:Key="DisabledBorderDarkColor">#FF888888</Color>

        <Color x:Key="DefaultBorderBrushDarkColor">Black</Color>

        <!--Control-specific resources.-->
        <Color x:Key="HeaderTopColor">#FFC5CBF9</Color>
        <Color x:Key="DatagridCurrentCellBorderColor">Black</Color>
        <Color x:Key="SliderTrackDarkColor">#FFC5CBF9</Color>

        <Color x:Key="NavButtonFrameColor">#FF3843C4</Color>

        <LinearGradientBrush x:Key="MenuPopupBrush"
                     EndPoint="0.5,1"
                     StartPoint="0.5,0">
            <GradientStop Color="{DynamicResource ControlLightColor}"
                Offset="0" />
            <GradientStop Color="{DynamicResource ControlMediumColor}"
                Offset="0.5" />
            <GradientStop Color="{DynamicResource ControlLightColor}"
                Offset="1" />
        </LinearGradientBrush>

        <LinearGradientBrush x:Key="ProgressBarIndicatorAnimatedFill"
                     StartPoint="0,0"
                     EndPoint="1,0">
            <LinearGradientBrush.GradientStops>
                <GradientStopCollection>
                    <GradientStop Color="#000000FF"
                    Offset="0" />
                    <GradientStop Color="#600000FF"
                    Offset="0.4" />
                    <GradientStop Color="#600000FF"
                    Offset="0.6" />
                    <GradientStop Color="#000000FF"
                    Offset="1" />
                </GradientStopCollection>
            </LinearGradientBrush.GradientStops>
        </LinearGradientBrush>

        <Style x:Key="{x:Type Menu}"
       TargetType="{x:Type Menu}">
            <Setter Property="OverridesDefaultStyle"
          Value="True" />
            <Setter Property="SnapsToDevicePixels"
          Value="True" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Menu}">
                        <Border BorderThickness="1">
                            <Border.BorderBrush>
                                <LinearGradientBrush StartPoint="0,0"
                                 EndPoint="0,1">
                                    <LinearGradientBrush.GradientStops>
                                        <GradientStopCollection>
                                            <GradientStop Color="{DynamicResource BorderLightColor}"
                                Offset="0.0" />
                                            <GradientStop Color="{DynamicResource BorderDarkColor}"
                                Offset="1.0" />
                                        </GradientStopCollection>
                                    </LinearGradientBrush.GradientStops>
                                </LinearGradientBrush>

                            </Border.BorderBrush>
                            <Border.Background>
                                <LinearGradientBrush EndPoint="0.5,1"
                                 StartPoint="0.5,0">
                                    <GradientStop Color="{DynamicResource ControlLightColor}"
                            Offset="0" />
                                    <GradientStop Color="{DynamicResource ControlMediumColor}"
                            Offset="1" />
                                </LinearGradientBrush>
                            </Border.Background>
                            <StackPanel ClipToBounds="True"
                      Orientation="Horizontal"
                      IsItemsHost="True" />
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

       <Style x:Key="{x:Static MenuItem.SeparatorStyleKey}"
       TargetType="{x:Type Separator}">
  <Setter Property="Height"
          Value="1" />
  <Setter Property="Margin"
          Value="0,4,0,4" />
  <Setter Property="Template">
    <Setter.Value>
      <ControlTemplate TargetType="{x:Type Separator}">
        <Border BorderThickness="1">
          <Border.BorderBrush>
            <SolidColorBrush Color="{DynamicResource BorderMediumColor}" />
          </Border.BorderBrush>
        </Border>
      </ControlTemplate>
    </Setter.Value>
  </Setter>
</Style>

        <!--ScrollViewer for a MenuItem-->
        <MenuScrollingVisibilityConverter x:Key="MenuScrollingVisibilityConverter" />

        <Style x:Key="MenuScrollViewer"
       TargetType="{x:Type ScrollViewer}"
       BasedOn="{x:Null}">
            <Setter Property="HorizontalScrollBarVisibility"
          Value="Hidden" />
            <Setter Property="VerticalScrollBarVisibility"
          Value="Auto" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ScrollViewer}">
                        <Grid SnapsToDevicePixels="True">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*" />
                            </Grid.ColumnDefinitions>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto" />
                                <RowDefinition Height="*" />
                                <RowDefinition Height="Auto" />
                            </Grid.RowDefinitions>
                            <Border Grid.Row="1"
                  Grid.Column="0">
                                <ScrollContentPresenter Margin="{TemplateBinding Padding}" />
                            </Border>

                            <!--Style="{StaticResource MenuScrollButton}"-->
                            <RepeatButton
                        Grid.Row="0"
                        Grid.Column="0"
                        Command="{x:Static ScrollBar.LineUpCommand}"
                        CommandTarget="{Binding RelativeSource={RelativeSource TemplatedParent}}"
                        Focusable="False">
                                <RepeatButton.Visibility>
                                    <MultiBinding FallbackValue="Visibility.Collapsed"
                            Converter="{StaticResource MenuScrollingVisibilityConverter}"
                            ConverterParameter="0">
                                        <Binding RelativeSource="{RelativeSource TemplatedParent}"
                         Path="ComputedVerticalScrollBarVisibility" />
                                        <Binding RelativeSource="{RelativeSource TemplatedParent}"
                         Path="VerticalOffset" />
                                        <Binding RelativeSource="{RelativeSource TemplatedParent}"
                         Path="ExtentHeight" />
                                        <Binding RelativeSource="{RelativeSource TemplatedParent}"
                         Path="ViewportHeight" />
                                    </MultiBinding>
                                </RepeatButton.Visibility>
                                <!--<Path Fill="{DynamicResource {x:Static SystemColors.MenuTextBrushKey}}"
                  Data="{StaticResource UpArrow}" />-->
                            </RepeatButton>

                            <!--Style="{StaticResource MenuScrollButton}"-->
                            <RepeatButton
                        Grid.Row="2"
                        Grid.Column="0"
                        Command="{x:Static ScrollBar.LineDownCommand}"
                        CommandTarget="{Binding RelativeSource={RelativeSource TemplatedParent}}"
                        Focusable="False">
                                <RepeatButton.Visibility>
                                    <MultiBinding FallbackValue="Visibility.Collapsed"
                            Converter="{StaticResource MenuScrollingVisibilityConverter}"
                            ConverterParameter="100">
                                        <Binding RelativeSource="{RelativeSource TemplatedParent}"
                         Path="ComputedVerticalScrollBarVisibility" />
                                        <Binding RelativeSource="{RelativeSource TemplatedParent}"
                         Path="VerticalOffset" />
                                        <Binding RelativeSource="{RelativeSource TemplatedParent}"
                         Path="ExtentHeight" />
                                        <Binding RelativeSource="{RelativeSource TemplatedParent}"
                         Path="ViewportHeight" />
                                    </MultiBinding>
                                </RepeatButton.Visibility>
                                <Path Fill="{DynamicResource {x:Static SystemColors.MenuTextBrushKey}}"
                   />
                                <!--Data="{StaticResource DownArrow}"-->
                            </RepeatButton>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

        <!-- TopLevelHeader -->
<ControlTemplate x:Key="{x:Static MenuItem.TopLevelHeaderTemplateKey}"
                 TargetType="{x:Type MenuItem}">
  <Border x:Name="Border">
    <Grid>
      <ContentPresenter Margin="6,3,6,3"
                        ContentSource="Header"
                        RecognizesAccessKey="True" />
      <Popup x:Name="Popup"
             Placement="Bottom"
             IsOpen="{TemplateBinding IsSubmenuOpen}"
             AllowsTransparency="True"
             Focusable="False"
             PopupAnimation="Fade">
        <Border x:Name="SubmenuBorder"
                SnapsToDevicePixels="True"
                BorderThickness="1"
                Background="{DynamicResource MenuPopupBrush}">
          <Border.BorderBrush>
            <SolidColorBrush Color="{DynamicResource BorderMediumColor}" />
          </Border.BorderBrush>
          <ScrollViewer CanContentScroll="True"
                        Style="{StaticResource MenuScrollViewer}">
            <StackPanel IsItemsHost="True"
                        KeyboardNavigation.DirectionalNavigation="Cycle" />
          </ScrollViewer>
        </Border>
      </Popup>
    </Grid>
  </Border>
  <ControlTemplate.Triggers>
    <Trigger Property="IsSuspendingPopupAnimation"
             Value="true">
      <Setter TargetName="Popup"
              Property="PopupAnimation"
              Value="None" />
    </Trigger>
    <Trigger Property="IsHighlighted"
             Value="true">
      <Setter TargetName="Border"
              Property="BorderBrush"
              Value="Transparent" />
      <Setter Property="Background"
              TargetName="Border">
        <Setter.Value>
          <LinearGradientBrush StartPoint="0,0"
                               EndPoint="0,1">
            <LinearGradientBrush.GradientStops>
              <GradientStopCollection>
                <GradientStop Color="{StaticResource ControlLightColor}" />
                <GradientStop Color="{StaticResource ControlMouseOverColor}"
                              Offset="1.0" />
              </GradientStopCollection>
            </LinearGradientBrush.GradientStops>
          </LinearGradientBrush>

        </Setter.Value>
      </Setter>
    </Trigger>
    <Trigger SourceName="Popup"
             Property="AllowsTransparency"
             Value="True">
      <Setter TargetName="SubmenuBorder"
              Property="CornerRadius"
              Value="0,0,4,4" />
      <Setter TargetName="SubmenuBorder"
              Property="Padding"
              Value="0,0,0,3" />
    </Trigger>
    <Trigger Property="IsEnabled"
             Value="False">
      <Setter Property="Foreground">
        <Setter.Value>
          <SolidColorBrush Color="{StaticResource DisabledForegroundColor}" />
        </Setter.Value>
      </Setter>
    </Trigger>
  </ControlTemplate.Triggers>
</ControlTemplate>

<!-- TopLevelItem -->
<ControlTemplate x:Key="{x:Static MenuItem.TopLevelItemTemplateKey}"
                 TargetType="{x:Type MenuItem}">
  <Border x:Name="Border">
    <Grid>
      <ContentPresenter Margin="6,3,6,3"
                        ContentSource="Header"
                        RecognizesAccessKey="True" />
    </Grid>
  </Border>
  <ControlTemplate.Triggers>
    <Trigger Property="IsHighlighted"
             Value="true">
      <Setter Property="Background"
              TargetName="Border">
        <Setter.Value>
          <LinearGradientBrush StartPoint="0,0"
                               EndPoint="0,1">
            <LinearGradientBrush.GradientStops>
              <GradientStopCollection>
                <GradientStop Color="{StaticResource ControlLightColor}" />
                <GradientStop Color="{StaticResource ControlMouseOverColor}"
                              Offset="1.0" />
              </GradientStopCollection>
            </LinearGradientBrush.GradientStops>
          </LinearGradientBrush>

        </Setter.Value>
      </Setter>
    </Trigger>
    <Trigger Property="IsEnabled"
             Value="False">
      <Setter Property="Foreground">
        <Setter.Value>
          <SolidColorBrush Color="{StaticResource DisabledForegroundColor}" />
        </Setter.Value>
      </Setter>
    </Trigger>
  </ControlTemplate.Triggers>
</ControlTemplate>

<!-- SubmenuItem -->
<ControlTemplate x:Key="{x:Static MenuItem.SubmenuItemTemplateKey}"
                 TargetType="{x:Type MenuItem}">
  <Border x:Name="Border"
          BorderThickness="1">
    <Grid>
      <Grid.ColumnDefinitions>
        <!--<ColumnDefinition Width="Auto"
                          SharedSizeGroup="Icon" />-->
        <ColumnDefinition Width="*" />
        <!--<ColumnDefinition Width="Auto"
                          SharedSizeGroup="Shortcut" />
        <ColumnDefinition Width="13" />-->
      </Grid.ColumnDefinitions>
      <ContentPresenter x:Name="Icon"
                        Margin="6,0,6,0"
                        VerticalAlignment="Center"
                        ContentSource="Icon" />
      <Border x:Name="Check"
              Width="13"
              Height="13"
              Visibility="Collapsed"
              Margin="6,0,6,0"
              BorderThickness="1">
        <Border.BorderBrush>
          <LinearGradientBrush StartPoint="0,0"
                               EndPoint="0,1">
            <LinearGradientBrush.GradientStops>
              <GradientStopCollection>
                <GradientStop Color="{DynamicResource BorderLightColor}"
                              Offset="0.0" />
                <GradientStop Color="{DynamicResource BorderDarkColor}"
                              Offset="1.0" />
              </GradientStopCollection>
            </LinearGradientBrush.GradientStops>
          </LinearGradientBrush>

        </Border.BorderBrush>
        <Border.Background>
          <LinearGradientBrush StartPoint="0,0"
                               EndPoint="0,1">
            <LinearGradientBrush.GradientStops>
              <GradientStopCollection>
                <GradientStop Color="{DynamicResource ControlLightColor}" />
                <GradientStop Color="{DynamicResource ControlMediumColor}"
                              Offset="1.0" />
              </GradientStopCollection>
            </LinearGradientBrush.GradientStops>
          </LinearGradientBrush>
        </Border.Background>
        <Path x:Name="CheckMark"
              Width="7"
              Height="7"
              Visibility="Hidden"
              SnapsToDevicePixels="False"
              StrokeThickness="2"
              Data="M 0 0 L 7 7 M 0 7 L 7 0">
          <Path.Stroke>
            <SolidColorBrush Color="{DynamicResource GlyphColor}" />
          </Path.Stroke>
        </Path>
      </Border>
      <ContentPresenter x:Name="HeaderHost"
                        Grid.Column="1"
                        ContentSource="Header"
                        RecognizesAccessKey="True" />
      <TextBlock x:Name="InputGestureText"
                 Grid.Column="2"
                 Text="{TemplateBinding InputGestureText}"
                 Margin="5,2,0,2"
                 DockPanel.Dock="Right" />
    </Grid>
  </Border>
  <ControlTemplate.Triggers>
    <Trigger Property="ButtonBase.Command"
             Value="{x:Null}" />
    <Trigger Property="Icon"
             Value="{x:Null}">
      <Setter TargetName="Icon"
              Property="Visibility"
              Value="Hidden" />
    </Trigger>
    <Trigger Property="IsChecked"
             Value="true">
      <Setter TargetName="CheckMark"
              Property="Visibility"
              Value="Visible" />
    </Trigger>
    <Trigger Property="IsCheckable"
             Value="true">
      <Setter TargetName="Check"
              Property="Visibility"
              Value="Visible" />
      <Setter TargetName="Icon"
              Property="Visibility"
              Value="Hidden" />
    </Trigger>
    <Trigger Property="IsHighlighted"
             Value="true">
      <Setter Property="Background"
              TargetName="Border">
        <Setter.Value>
          <LinearGradientBrush EndPoint="0.5,1"
                               StartPoint="0.5,0">
            <GradientStop Color="Transparent"
                          Offset="0" />
            <GradientStop Color="{DynamicResource ControlMouseOverColor}"
                          Offset="1" />
          </LinearGradientBrush>
        </Setter.Value>
      </Setter>
      <Setter Property="BorderBrush"
              TargetName="Border">
        <Setter.Value>
          <LinearGradientBrush EndPoint="0.5,1"
                               StartPoint="0.5,0">
            <GradientStop Color="{DynamicResource BorderMediumColor}"
                          Offset="0" />
            <GradientStop Color="Transparent"
                          Offset="1" />
          </LinearGradientBrush>
        </Setter.Value>
      </Setter>
    </Trigger>
    <Trigger Property="IsEnabled"
             Value="false">
      <Setter Property="Foreground">
        <Setter.Value>
          <SolidColorBrush Color="{StaticResource DisabledForegroundColor}" />
        </Setter.Value>
      </Setter>
    </Trigger>
  </ControlTemplate.Triggers>
</ControlTemplate>

<ControlTemplate x:Key="{x:Static MenuItem.SubmenuHeaderTemplateKey}"
                 TargetType="{x:Type MenuItem}">
  <Border x:Name="Border"
          BorderThickness="1">
    <Grid>
      <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto"
                          SharedSizeGroup="Icon" />
        <ColumnDefinition Width="*" />
        <ColumnDefinition Width="Auto"
                          SharedSizeGroup="Shortcut" />
        <ColumnDefinition Width="13" />
      </Grid.ColumnDefinitions>
      <ContentPresenter x:Name="Icon"
                        Margin="6,0,6,0"
                        VerticalAlignment="Center"
                        ContentSource="Icon" />
      <ContentPresenter x:Name="HeaderHost"
                        Grid.Column="1"
                        ContentSource="Header"
                        RecognizesAccessKey="True" />
      <TextBlock x:Name="InputGestureText"
                 Grid.Column="2"
                 Text="{TemplateBinding InputGestureText}"
                 Margin="5,2,2,2"
                 DockPanel.Dock="Right" />
      <Path Grid.Column="3"
            HorizontalAlignment="Center"
            VerticalAlignment="Center"
            Data="M 0 0 L 0 7 L 4 3.5 Z">
        <Path.Fill>
          <SolidColorBrush Color="{DynamicResource GlyphColor}" />
        </Path.Fill>
      </Path>
      <Popup x:Name="Popup"
             Placement="Right"
             HorizontalOffset="-4"
             IsOpen="{TemplateBinding IsSubmenuOpen}"
             AllowsTransparency="True"
             Focusable="False"
             PopupAnimation="Fade">
        <Border x:Name="SubmenuBorder"
                SnapsToDevicePixels="True"
                Background="{DynamicResource MenuPopupBrush}"
                BorderThickness="1">
          <Border.BorderBrush>
            <SolidColorBrush Color="{DynamicResource BorderMediumColor}" />
          </Border.BorderBrush>
          <ScrollViewer CanContentScroll="True"
                        Style="{StaticResource MenuScrollViewer}">
            <StackPanel IsItemsHost="True"
                        KeyboardNavigation.DirectionalNavigation="Cycle" />
          </ScrollViewer>
        </Border>
      </Popup>
    </Grid>
  </Border>
  <ControlTemplate.Triggers>
    <Trigger Property="Icon"
             Value="{x:Null}">
      <Setter TargetName="Icon"
              Property="Visibility"
              Value="Collapsed" />
    </Trigger>
    <Trigger Property="IsHighlighted"
             Value="true">
      <Setter Property="Background"
              TargetName="Border">
        <Setter.Value>
          <LinearGradientBrush EndPoint="0.5,1"
                               StartPoint="0.5,0">
            <GradientStop Color="Transparent"
                          Offset="0" />
            <GradientStop Color="{DynamicResource ControlMouseOverColor}"
                          Offset="1" />
          </LinearGradientBrush>
        </Setter.Value>
      </Setter>
      <Setter Property="BorderBrush"
              TargetName="Border">
        <Setter.Value>
          <LinearGradientBrush EndPoint="0.5,1"
                               StartPoint="0.5,0">
            <GradientStop Color="{DynamicResource BorderMediumColor}"
                          Offset="0" />
            <GradientStop Color="Transparent"
                          Offset="1" />
          </LinearGradientBrush>
        </Setter.Value>
      </Setter>
    </Trigger>
    <Trigger SourceName="Popup"
             Property="AllowsTransparency"
             Value="True">
      <Setter TargetName="SubmenuBorder"
              Property="CornerRadius"
              Value="4" />
      <Setter TargetName="SubmenuBorder"
              Property="Padding"
              Value="0,3,0,3" />
    </Trigger>
    <Trigger Property="IsEnabled"
             Value="false">
      <Setter Property="Foreground">
        <Setter.Value>
          <SolidColorBrush Color="{StaticResource DisabledForegroundColor}" />
        </Setter.Value>
      </Setter>
    </Trigger>
  </ControlTemplate.Triggers>
</ControlTemplate>

<!-- MenuItem Style -->
<Style x:Key="{x:Type MenuItem}"
       TargetType="{x:Type MenuItem}">
  <Setter Property="OverridesDefaultStyle"
          Value="True" />
  <Style.Triggers>
    <Trigger Property="Role"
             Value="TopLevelHeader">
      <Setter Property="Template"
              Value="{StaticResource {x:Static MenuItem.TopLevelHeaderTemplateKey}}" />
      <Setter Property="Grid.IsSharedSizeScope"
              Value="true" />
    </Trigger>
    <Trigger Property="Role"
             Value="TopLevelItem">
      <Setter Property="Template"
              Value="{StaticResource {x:Static MenuItem.TopLevelItemTemplateKey}}" />
    </Trigger>
    <Trigger Property="Role"
             Value="SubmenuHeader">
      <Setter Property="Template"
              Value="{StaticResource {x:Static MenuItem.SubmenuHeaderTemplateKey}}" />
    </Trigger>
    <Trigger Property="Role"
             Value="SubmenuItem">
      <Setter Property="Template"
              Value="{StaticResource {x:Static MenuItem.SubmenuItemTemplateKey}}" />
    </Trigger>
  </Style.Triggers>
</Style>

        <XmlDataProvider x:Key="menudata" Source="Config\Tools\MenuData.xml" XPath="MenuData/Operation"></XmlDataProvider>
        <HierarchicalDataTemplate DataType="Operation"
                                  ItemsSource="{Binding XPath=Operation}">
            <StackPanel Orientation="Horizontal" >
                <TextBlock Text="{Binding [email protected]}" HorizontalAlignment="Left"></TextBlock>
                <TextBlock Text="{Binding [email protected]}" HorizontalAlignment="Left"></TextBlock>
            </StackPanel>

        </HierarchicalDataTemplate>
    </Window.Resources>

时间: 2024-10-20 08:54:01

WPF:MenuItem样式的相关文章

WPF自定义样式篇-DataGrid

WPF自定义样式篇-DataGrid 先上效果图: 样式: <!--DataGrid样式-->    <Style TargetType="DataGrid">        <Setter Property="RowHeaderWidth" Value="0"></Setter>        <Setter Property="AutoGenerateColumns"

求助 WPF ListViewItem样式问题

求助 WPF ListViewItem样式问题 .NET 开发 > Windows Presentation Foundation Вопрос 0 Нужно войти <Style TargetType="ListViewItem"> <!--<Setter Property="Margin" Value="0,1,0,0"/>--> <Setter Property="Height

WPF设置样式的几种方式

第一种方式是直接使用Setter来进行,可以对Background等进行设置. <Window.Resources> <Style TargetType="Button"> <Setter Property="Background" Value="Red"/> </Style></Window.Resources> 第二种是直接将比较复杂一点的Style放置到Window.Resourc

WPF DataGrid 样式设置

隔行换色,鼠标单击,悬浮样式都有,其具体效果如图 1 所示. 图 1 WPF DataGrid 样式设置效果图 其中: 界面设计代码下所示 ? + 查看代码 1 2 3 4 5 6 7 8 9 10 11 12 <DataGrid AutoGenerateColumns="False"  Name="dataGrid1"  VerticalAlignment="Top"       CanUserSortColumns="Fals

WPF中样式和行为和触发器

原文:WPF中样式和行为和触发器 样式简介:样式(style)是组织和重用格式化选项的重要工具,不是使用重复的标记填充XAML,以便设置外边距.内边距.颜色以及字体等细节.而是创建一系列封装所有这些细节的样式,然后在需要之处通过属性来设置样式.名称空间是System.Windows. 1.样式. 为了理解适合使用样式的集合,分析一个简单的示例,设想需要标准化在窗口中使用的字体,最简单的方式是设置包含窗口的字体属性,这些属性都是在Control类中定义的,包括FontFamily.FontSize

wpf menuitem 简约显示的 template样式

<ControlTemplate x:Key="MenuItemControlTemplate1" TargetType="{x:Type MenuItem}"> <Grid SnapsToDevicePixels="True"> <Border x:Name="OuterBorder" BorderThickness="0,0,0,0.7" BorderBrush="

WPF之样式——style

wpf这门编写winform程序的技术相比常规的winform技术有一个很显著的特点--炫! 怎么才能炫起来呢? 我们都知道常规的winform技术就是拖控件,总的来说想用常规的winform技术来实现很好看的界面很难. 但是wpf就可以实现起来很轻松. 有过web知识的童鞋很好理解,咱们看到的网站大部分都比winfrom好看的多,为啥? 因为里面所有的控件都是程序人员拿HTML编写的,编程人员可以自由的定义控件的样式.我们的WPF一样可以轻松的定义控件的样式. 下面我们一起来体验一下: 我们建

WPF编程—样式

WPF编程学习——样式 本文目录 1.引言 2.怎样使用样式? 3.内联样式 4.已命名样式 5.元素类型样式 6.编程控制样式 7.触发器 1.引言 样式(Style),主要是用来让元素或内容呈现一定外观的属性.WPF中的样式的作用,就像Web中的CSS一样,为界面上的元素定制外观,以提供更好的用户界面.在WPF应用程序中,通过控件的属性,我们也可以实现更改控件的外观.但是,这种方式局限性大.不灵活且不利于维护.例如: 复制代码 <Grid> <Button Width="8

WPF Slider样式

简述 ?? WPF中Slider分为三部分,滑块类型为Thumb:进度条类型为RepeatButton,其中:位于滑块左侧或下侧部分为DecreaseRepeatButton,位于滑块右侧或上侧部分为IncreaseRepeatButton.?? 设置Slider样式时,一般单独设置三部分的样式(Style),然后在Sldier的样式(Style)中引用它们. 代码示例 <Style TargetType="{x:Type Thumb}" x:Key="SliderTh