<!-- Simple Menu - This template uses a StackPanel to layout MenuItems --> <Style x:Key="SimpleMenu" TargetType="{x:Type Menu}"> <Setter Property="Background" Value="{DynamicResource LightBrush}"/> <Setter Property="BorderBrush" Value="{DynamicResource NormalBorderBrush}"/> <Setter Property="SnapsToDevicePixels" Value="True"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type Menu}"> <Grid> <Border Margin="1" x:Name="Border" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}"/> <StackPanel Background="{TemplateBinding Background}" IsItemsHost="True" ClipToBounds="True" Orientation="Horizontal"/> </Grid> <ControlTemplate.Triggers> <Trigger Property="IsEnabled" Value="False"> <Setter Property="Background" Value="{DynamicResource DisabledBackgroundBrush}" TargetName="Border"/> <Setter Property="BorderBrush" Value="{DynamicResource DisabledBorderBrush}" TargetName="Border"/> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style><!-- Simple MenuItem - The template uses triggers to provide four different arrangements of menu item which are set via the Role property --> <Style x:Key="SimpleMenuItem" TargetType="{x:Type MenuItem}"> <Setter Property="HorizontalContentAlignment" Value="{Binding Path=HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/> <Setter Property="VerticalContentAlignment" Value="{Binding Path=VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/> <Setter Property="Background" Value="Transparent"/> <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.MenuTextBrushKey}}"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type MenuItem}"> <Border x:Name="Border" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}"> <Grid> <!-- The Grid is used to hold together columns for an Icon, Content, Glyph checkmark and Arrow to show the next level Size sharing is used in Grid so that the Icon, Content, Arrow for each MenuItem align together --> <Grid.ColumnDefinitions> <ColumnDefinition MinWidth="17" Width="Auto" SharedSizeGroup="MenuItemIconColumnGroup"/> <ColumnDefinition Width="*"/> <ColumnDefinition Width="Auto" SharedSizeGroup="MenuItemIGTColumnGroup"/> <ColumnDefinition Width="14"/> </Grid.ColumnDefinitions> <!-- ContentPresenter to show an Icon if needed --> <ContentPresenter Margin="4,0,6,0" x:Name="Icon" VerticalAlignment="Center" ContentSource="Icon"/> <!-- Glyph is a checkmark if needed for a checkable menu --> <Grid 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"/> <!-- 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" 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" AllowsTransparency="true" PopupAnimation="{DynamicResource {x:Static SystemParameters.MenuPopupAnimationKey}}" VerticalOffset="-3"> <Grid x:Name="SubMenu"> <Border x:Name="SubMenuBorder" Background="{DynamicResource WindowBackgroundBrush}" BorderBrush="{DynamicResource SolidBorderBrush}" BorderThickness="1"/> <!-- StackPanel holds children of the menu. This is set bu IsItemsHost=True --> <StackPanel IsItemsHost="True" KeyboardNavigation.DirectionalNavigation="Cycle"/> </Grid> </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="Margin" Value="0,1,0,1"/> <Setter Property="Padding" Value="6,3,6,3"/> <Setter Property="Grid.IsSharedSizeScope" Value="true"/> <Setter Property="Placement" Value="Bottom" TargetName="SubMenuPopup"/> <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="Margin" Value="0,1,0,1"/> <Setter Property="Padding" Value="6,3,6,3"/> <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"/> <Setter Property="Grid.IsSharedSizeScope" Value="true"/> </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> <Trigger Property="AllowsTransparency" SourceName="SubMenuPopup" Value="true"> <Setter Property="Margin" Value="0,0,3,3" TargetName="SubMenu"/> <Setter Property="SnapsToDevicePixels" Value="true" TargetName="SubMenu"/> <Setter Property="BitmapEffect" Value="{DynamicResource PopupDropShadow}" TargetName="SubMenuBorder"/> </Trigger> <!-- Using the system colors for the Menu Highlight and IsEnabled--> <Trigger Property="IsHighlighted" Value="true"> <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}" TargetName="Border"/> <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}"/> </Trigger> <Trigger Property="IsEnabled" Value="false"> <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style>