C#实现Menu和ContextMenu自定义风格及contextMenu自定义

2019-12-26 13:08:31王振洲
  • Style="{DynamicResource {ComponentResourceKey ResourceId=MenuScrollViewer, TypeInTargetAssembly={x:Type FrameworkElement}}}">  <ItemsPresenter KeyboardNavigation.DirectionalNavigation="Cycle" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" Uid="ItemsPresenter_5"/> 
  • </ScrollViewer>  </Border> 
  • </Border>  </ControlTemplate> 
  • </Setter.Value>  </Setter> 
  • </Style> 

    2. 针对其中的ItemContainerStyle来写个MenuItem的control template

     

     
    1. <Style x:Key="MenuItemStyle1" TargetType="{x:Type MenuItem}"> <Setter Property="Template" Value="{DynamicResource MenuItemControlTemplate1}"/> <Setter Property="Margin" Value="0"></Setter> <Setter Property="Padding" Value="0"></Setter> </Style> <ControlTemplate x:Key="MenuItemControlTemplate1" TargetType="{x:Type MenuItem}"> <Grid x:Name="grid" SnapsToDevicePixels="True" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" > <ContentPresenter ContentTemplate="{TemplateBinding HeaderTemplate}" Content="{TemplateBinding Header}" Grid.Column="0" ContentStringFormat="{TemplateBinding HeaderStringFormat}" ContentSource="Header" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/> </Grid> <ControlTemplate.Triggers> <Trigger Property="IsHighlighted" Value="True"> <Setter Property="Background" TargetName="grid" Value="{DynamicResource Brush_PA_CSW_ListBoxItemDefaultHighlight}"/> </Trigger> <Trigger Property="IsEnabled" Value="False"> <Setter Property="Foreground" Value="#FF9A9A9A"/> </Trigger> </ControlTemplate.Triggers> </ControlTemplate>  3. contextMenu使用上述style