ContextMenuStyle.xaml 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  2. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  3. xmlns:sys="clr-namespace:System;assembly=mscorlib">
  4. <SolidColorBrush x:Key="MenuItem.MouseOver.Background">#E2E2E2</SolidColorBrush>
  5. <SolidColorBrush x:Key="MenuItem.MouseOver.Foreground">#000000</SolidColorBrush>
  6. <SolidColorBrush x:Key="ContextMenu.BorderBrush">#C4C4C4</SolidColorBrush>
  7. <!-- 设置阴影参数 -->
  8. <Thickness x:Key="ContextMenu.Shadow.Grid.Margin">12</Thickness>
  9. <sys:Double x:Key="ContextMenu.Shadow.BlurRadius">12</sys:Double>
  10. <Style x:Key="MaterialDesignMenu" TargetType="{x:Type MenuBase}">
  11. <Setter Property="Background" Value="#FFFFFF" />
  12. <Setter Property="Foreground" Value="{DynamicResource MaterialDesignBody}" />
  13. <Setter Property="TextBlock.FontSize" Value="12" />
  14. <Setter Property="VerticalContentAlignment" Value="Center" />
  15. <Setter Property="Template">
  16. <Setter.Value>
  17. <ControlTemplate TargetType="{x:Type MenuBase}">
  18. <Border Background="{TemplateBinding Background}"
  19. BorderBrush="{TemplateBinding BorderBrush}"
  20. BorderThickness="{TemplateBinding BorderThickness}"
  21. Padding="{TemplateBinding Padding}"
  22. SnapsToDevicePixels="true">
  23. <ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
  24. </Border>
  25. </ControlTemplate>
  26. </Setter.Value>
  27. </Setter>
  28. </Style>
  29. <Style BasedOn="{x:Null}" TargetType="{x:Type MenuItem}">
  30. <Setter Property="Padding" Value="20 0 20 0" />
  31. <Setter Property="OverridesDefaultStyle" Value="True" />
  32. <Setter Property="Background" Value="#FFFFFF" />
  33. <Setter Property="Template">
  34. <Setter.Value>
  35. <ControlTemplate TargetType="{x:Type MenuItem}">
  36. <Grid ClipToBounds="True">
  37. <Border x:Name="templateRoot"
  38. Background="{TemplateBinding Background}"
  39. BorderBrush="{TemplateBinding BorderBrush}"
  40. BorderThickness="{TemplateBinding BorderThickness}"
  41. SnapsToDevicePixels="True" />
  42. <Border x:Name="BackgroundRoot"
  43. Background="{TemplateBinding Background}"
  44. BorderBrush="{TemplateBinding BorderBrush}"
  45. BorderThickness="{TemplateBinding BorderThickness}"
  46. Opacity="0" SnapsToDevicePixels="True" />
  47. <Grid Height="{TemplateBinding Height}" Background="Transparent">
  48. <Grid Margin="{TemplateBinding Padding}"
  49. VerticalAlignment="Center" Background="Transparent">
  50. <Grid.ColumnDefinitions>
  51. <ColumnDefinition Width="Auto" SharedSizeGroup="IconGroup" />
  52. <ColumnDefinition Width="*" SharedSizeGroup="HeaderGroup" />
  53. <ColumnDefinition Width="*" />
  54. </Grid.ColumnDefinitions>
  55. <Grid x:Name="IconWrapper" Width="40" Visibility="Visible">
  56. <ContentPresenter x:Name="Icon" Width="16" Height="10" HorizontalAlignment="Left"
  57. VerticalAlignment="Center"
  58. Content="{TemplateBinding Icon}"
  59. ContentSource="Icon"
  60. SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
  61. </Grid>
  62. <Grid Grid.Column="1" Background="Transparent">
  63. <ContentPresenter x:Name="BoldHeaderPresenter" VerticalAlignment="Center"
  64. Content="{TemplateBinding Header}"
  65. ContentSource="Header"
  66. ContentStringFormat="{TemplateBinding HeaderStringFormat}"
  67. ContentTemplate="{TemplateBinding HeaderTemplate}"
  68. RecognizesAccessKey="True"
  69. SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
  70. TextBlock.FontWeight="Normal" Visibility="Hidden" />
  71. <ContentPresenter x:Name="HeaderPresenter" VerticalAlignment="Center"
  72. Content="{TemplateBinding Header}"
  73. ContentSource="Header"
  74. ContentStringFormat="{TemplateBinding HeaderStringFormat}"
  75. ContentTemplate="{TemplateBinding HeaderTemplate}"
  76. RecognizesAccessKey="True"
  77. SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
  78. </Grid>
  79. <Grid x:Name="SubBlock" Grid.Column="2" Margin="16 0 0 0" Visibility="Collapsed">
  80. <Path Width="5" Height="10" HorizontalAlignment="Right" VerticalAlignment="Center"
  81. Data="M0,0 5,5 0,10" SnapsToDevicePixels="False" Stretch="Uniform"
  82. Stroke="{TemplateBinding Foreground}"
  83. StrokeThickness="1" />
  84. </Grid>
  85. </Grid>
  86. </Grid>
  87. <Popup x:Name="PART_Popup" AllowsTransparency="True" Focusable="False" HorizontalOffset="-2"
  88. IsOpen="{Binding IsSubmenuOpen, RelativeSource={RelativeSource TemplatedParent}}"
  89. Placement="Bottom" PopupAnimation="None">
  90. <Popup.CacheMode>
  91. <BitmapCache EnableClearType="True" SnapsToDevicePixels="True" />
  92. </Popup.CacheMode>
  93. <Grid Margin="{StaticResource ContextMenu.Shadow.Grid.Margin}">
  94. <Border Background="#FFFFFF" CornerRadius="2" SnapsToDevicePixels="True" UseLayoutRounding="True">
  95. <Border.Effect>
  96. <DropShadowEffect BlurRadius="{StaticResource ContextMenu.Shadow.BlurRadius}"
  97. Opacity="0.2" ShadowDepth="0" Color="Brown" />
  98. </Border.Effect>
  99. </Border>
  100. <Border x:Name="SubMenuBorder" Margin="0"
  101. Background="{Binding Path=Background, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=MenuBase}}"
  102. BorderBrush="{StaticResource ContextMenu.BorderBrush}"
  103. BorderThickness="1" CornerRadius="2"
  104. SnapsToDevicePixels="True"
  105. UseLayoutRounding="True">
  106. <Border Background="Transparent">
  107. <ScrollViewer x:Name="SubMenuScrollViewer" Style="{DynamicResource {ComponentResourceKey ResourceId=MenuScrollViewer, TypeInTargetAssembly={x:Type FrameworkElement}}}">
  108. <Grid Margin="0 1" RenderOptions.ClearTypeHint="Enabled">
  109. <Canvas Width="0" Height="0" HorizontalAlignment="Left" VerticalAlignment="Top">
  110. <Rectangle x:Name="OpaqueRect"
  111. Width="{Binding ActualWidth, ElementName=SubMenuBorder}"
  112. Height="{Binding ActualHeight, ElementName=SubMenuBorder}"
  113. Fill="{Binding Background, ElementName=SubMenuBorder}" />
  114. </Canvas>
  115. <ItemsPresenter x:Name="ItemsPresenter" Grid.IsSharedSizeScope="True"
  116. KeyboardNavigation.DirectionalNavigation="Cycle" KeyboardNavigation.TabNavigation="Cycle"
  117. SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
  118. </Grid>
  119. </ScrollViewer>
  120. </Border>
  121. </Border>
  122. </Grid>
  123. </Popup>
  124. </Grid>
  125. <ControlTemplate.Triggers>
  126. <!--#region Roles Triggers-->
  127. <Trigger Property="Role" Value="SubmenuHeader">
  128. <Setter TargetName="SubBlock" Property="Visibility" Value="Visible" />
  129. <Setter TargetName="PART_Popup" Property="Placement" Value="Right" />
  130. <Setter Property="Height" Value="28" />
  131. <Setter Property="Foreground" Value="Black" />
  132. <Setter TargetName="BoldHeaderPresenter" Property="Visibility" Value="Collapsed" />
  133. </Trigger>
  134. <Trigger Property="Role" Value="SubmenuItem">
  135. <Setter Property="Height" Value="28" />
  136. <Setter Property="Foreground" Value="Black" />
  137. <Setter TargetName="BoldHeaderPresenter" Property="Visibility" Value="Collapsed" />
  138. </Trigger>
  139. <Trigger Property="Role" Value="TopLevelHeader">
  140. <Setter Property="Grid.IsSharedSizeScope" Value="True" />
  141. <Setter Property="Padding" Value="16 0" />
  142. <Setter Property="Height" Value="28" />
  143. <Setter TargetName="templateRoot" Property="CornerRadius" Value="2" />
  144. <Setter TargetName="BackgroundRoot" Property="CornerRadius" Value="2" />
  145. </Trigger>
  146. <Trigger Property="Role" Value="TopLevelItem">
  147. <Setter Property="Padding" Value="16 0" />
  148. <Setter Property="Height" Value="28" />
  149. <Setter TargetName="templateRoot" Property="CornerRadius" Value="2" />
  150. <Setter TargetName="BackgroundRoot" Property="CornerRadius" Value="2" />
  151. </Trigger>
  152. <MultiTrigger>
  153. <MultiTrigger.Conditions>
  154. <Condition Property="Role" Value="TopLevelHeader" />
  155. <Condition Property="IsSubmenuOpen" Value="True" />
  156. </MultiTrigger.Conditions>
  157. <MultiTrigger.Setters>
  158. <Setter TargetName="HeaderPresenter" Property="TextBlock.FontWeight" Value="Normal" />
  159. </MultiTrigger.Setters>
  160. </MultiTrigger>
  161. <!--#endregion-->
  162. <Trigger SourceName="PART_Popup" Property="HasDropShadow" Value="True">
  163. <Setter TargetName="SubMenuBorder" Property="Margin" Value="0" />
  164. </Trigger>
  165. <Trigger Property="IsSuspendingPopupAnimation" Value="True">
  166. <Setter TargetName="PART_Popup" Property="PopupAnimation" Value="None" />
  167. </Trigger>
  168. <Trigger Property="Icon" Value="{x:Null}">
  169. <Setter TargetName="IconWrapper" Property="Visibility" Value="Collapsed" />
  170. </Trigger>
  171. <Trigger Property="IsCheckable" Value="True">
  172. <Setter TargetName="IconWrapper" Property="Visibility" Value="Collapsed" />
  173. </Trigger>
  174. <!--高亮状态-->
  175. <Trigger Property="IsHighlighted" Value="True">
  176. <Setter Property="Background" Value="{StaticResource MenuItem.MouseOver.Background}" />
  177. <Setter TargetName="BackgroundRoot" Property="Opacity" Value="0.13" />
  178. </Trigger>
  179. <Trigger Property="IsEnabled" Value="False">
  180. <Setter Property="Opacity" Value=".56" />
  181. </Trigger>
  182. <Trigger SourceName="SubMenuScrollViewer" Property="CanContentScroll" Value="False">
  183. <Setter TargetName="OpaqueRect" Property="Canvas.Top" Value="{Binding VerticalOffset, ElementName=SubMenuScrollViewer}" />
  184. <Setter TargetName="OpaqueRect" Property="Canvas.Left" Value="{Binding HorizontalOffset, ElementName=SubMenuScrollViewer}" />
  185. </Trigger>
  186. <Trigger Property="IsMouseOver" Value="True">
  187. <Setter Property="Background" Value="{StaticResource MenuItem.MouseOver.Background}" />
  188. <Setter Property="Foreground" Value="{StaticResource MenuItem.MouseOver.Foreground}" />
  189. </Trigger>
  190. </ControlTemplate.Triggers>
  191. </ControlTemplate>
  192. </Setter.Value>
  193. </Setter>
  194. </Style>
  195. <Style x:Key="DefaultContextMenuStyle" BasedOn="{StaticResource MaterialDesignMenu}" TargetType="{x:Type ContextMenu}">
  196. <Setter Property="Background" Value="Transparent" />
  197. <!--<Setter Property="ItemContainerStyle" Value="{StaticResource DefaultMenuItemStyle}" />-->
  198. <Setter Property="Template">
  199. <Setter.Value>
  200. <ControlTemplate TargetType="{x:Type MenuBase}">
  201. <AdornerDecorator>
  202. <AdornerDecorator.CacheMode>
  203. <BitmapCache EnableClearType="True" SnapsToDevicePixels="True" />
  204. </AdornerDecorator.CacheMode>
  205. <Grid Margin="{StaticResource ContextMenu.Shadow.Grid.Margin}">
  206. <Border Background="#FFFFFF" CornerRadius="2" SnapsToDevicePixels="True" UseLayoutRounding="True">
  207. <Border.Effect>
  208. <DropShadowEffect BlurRadius="{StaticResource ContextMenu.Shadow.BlurRadius}"
  209. Opacity="0.2" ShadowDepth="0" Color="Brown" />
  210. </Border.Effect>
  211. </Border>
  212. <Border Margin="0"
  213. Background="{TemplateBinding Background}"
  214. BorderBrush="{StaticResource ContextMenu.BorderBrush}"
  215. BorderThickness="1" CornerRadius="2"
  216. SnapsToDevicePixels="True"
  217. UseLayoutRounding="True">
  218. <ScrollViewer x:Name="SubMenuScrollViewer" Style="{DynamicResource {ComponentResourceKey ResourceId=MenuScrollViewer, TypeInTargetAssembly={x:Type FrameworkElement}}}">
  219. <Grid Margin="0 2" RenderOptions.ClearTypeHint="Enabled">
  220. <ItemsPresenter x:Name="ItemsPresenter" Grid.IsSharedSizeScope="True"
  221. KeyboardNavigation.DirectionalNavigation="Cycle"
  222. KeyboardNavigation.TabNavigation="Cycle"
  223. SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
  224. </Grid>
  225. </ScrollViewer>
  226. </Border>
  227. </Grid>
  228. </AdornerDecorator>
  229. </ControlTemplate>
  230. </Setter.Value>
  231. </Setter>
  232. </Style>
  233. </ResourceDictionary>