主頁 > .NET開發 > wpf MenuItem 默認樣式存檔,方便手頭沒有 vs 時查閱.

wpf MenuItem 默認樣式存檔,方便手頭沒有 vs 時查閱.

2022-04-21 06:10:06 .NET開發

  1         <SolidColorBrush
  2             x:Key="Menu.Static.Background"
  3             Color="#FFF0F0F0" />
  4         <SolidColorBrush
  5             x:Key="Menu.Static.Border"
  6             Color="#FF999999" />
  7         <SolidColorBrush
  8             x:Key="Menu.Static.Foreground"
  9             Color="#FF212121" />
 10         <SolidColorBrush
 11             x:Key="Menu.Static.Separator"
 12             Color="#FFD7D7D7" />
 13         <SolidColorBrush
 14             x:Key="Menu.Disabled.Foreground"
 15             Color="#FF707070" />
 16         <SolidColorBrush
 17             x:Key="MenuItem.Selected.Background"
 18             Color="#3D26A0DA" />
 19         <SolidColorBrush
 20             x:Key="MenuItem.Selected.Border"
 21             Color="#FF26A0DA" />
 22         <SolidColorBrush
 23             x:Key="MenuItem.Highlight.Background"
 24             Color="#3D26A0DA" />
 25         <SolidColorBrush
 26             x:Key="MenuItem.Highlight.Border"
 27             Color="#FF26A0DA" />
 28         <SolidColorBrush
 29             x:Key="MenuItem.Highlight.Disabled.Background"
 30             Color="#0A000000" />
 31         <SolidColorBrush
 32             x:Key="MenuItem.Highlight.Disabled.Border"
 33             Color="#21000000" />
 34         <MenuScrollingVisibilityConverter x:Key="MenuScrollingVisibilityConverter" />
 35         <Geometry x:Key="DownArrow">M 0,0 L 3.5,4 L 7,0 Z</Geometry>
 36         <Geometry x:Key="UpArrow">M 0,4 L 3.5,0 L 7,4 Z</Geometry>
 37         <Geometry x:Key="RightArrow">M 0,0 L 4,3.5 L 0,7 Z</Geometry>
 38         <Geometry x:Key="Checkmark">F1 M 10.0,1.2 L 4.7,9.1 L 4.5,9.1 L 0,5.2 L 1.3,3.5 L 4.3,6.1L 8.3,0 L 10.0,1.2 Z</Geometry>
 39 
 40         <Style
 41             x:Key="MenuScrollButton"
 42             BasedOn="{x:Null}"
 43             TargetType="{x:Type RepeatButton}">
 44             <Setter Property="ClickMode" Value="Hover" />
 45             <Setter Property="Template">
 46                 <Setter.Value>
 47                     <ControlTemplate TargetType="{x:Type RepeatButton}">
 48                         <Border
 49                             x:Name="templateRoot"
 50                             Background="Transparent"
 51                             BorderBrush="Transparent"
 52                             BorderThickness="1"
 53                             SnapsToDevicePixels="true">
 54                             <ContentPresenter
 55                                 Margin="6"
 56                                 HorizontalAlignment="Center"
 57                                 VerticalAlignment="Center" />
 58                         </Border>
 59                     </ControlTemplate>
 60                 </Setter.Value>
 61             </Setter>
 62         </Style>
 63 
 64 
 65         <!-- 彈出的子選單中 ScrollViewer 的樣式,用于在選單項較多時,上下滾動顯示所有的選單項 -->
 66         <Style
 67             x:Key="{ComponentResourceKey ResourceId=MenuScrollViewer,
 68                                          TypeInTargetAssembly={x:Type FrameworkElement}}"
 69             BasedOn="{x:Null}"
 70             TargetType="{x:Type ScrollViewer}">
 71             <Setter Property="HorizontalScrollBarVisibility" Value="Hidden" />
 72             <Setter Property="VerticalScrollBarVisibility" Value="Auto" />
 73             <Setter Property="Template">
 74                 <Setter.Value>
 75                     <ControlTemplate TargetType="{x:Type ScrollViewer}">
 76                         <Grid SnapsToDevicePixels="true">
 77                             <Grid.ColumnDefinitions>
 78                                 <ColumnDefinition Width="*" />
 79                             </Grid.ColumnDefinitions>
 80 
 81                             <Grid.RowDefinitions>
 82                                 <RowDefinition Height="Auto" />
 83                                 <RowDefinition Height="*" />
 84                                 <RowDefinition Height="Auto" />
 85                             </Grid.RowDefinitions>
 86 
 87                             <!-- 所有的選單內容 -->
 88                             <Border
 89                                 Grid.Row="1"
 90                                 Grid.Column="0">
 91                                 <ScrollContentPresenter
 92                                     Margin="{TemplateBinding Padding}"
 93                                     CanContentScroll="{TemplateBinding CanContentScroll}" />
 94                             </Border>
 95 
 96                             <!-- 向上滾動按鈕 -->
 97                             <RepeatButton
 98                                 Grid.Row="0"
 99                                 Grid.Column="0"
100                                 Command="{x:Static ScrollBar.LineUpCommand}"
101                                 CommandTarget="{Binding RelativeSource={RelativeSource TemplatedParent}}"
102                                 Focusable="false"
103                                 Style="{StaticResource MenuScrollButton}">
104                                 <RepeatButton.Visibility>
105                                     <MultiBinding
106                                         Converter="{StaticResource MenuScrollingVisibilityConverter}"
107                                         ConverterParameter="0"
108                                         FallbackValue="Visibility.Collapsed">
109                                         <Binding
110                                             Path="ComputedVerticalScrollBarVisibility"
111                                             RelativeSource="{RelativeSource TemplatedParent}" />
112                                         <Binding
113                                             Path="VerticalOffset"
114                                             RelativeSource="{RelativeSource TemplatedParent}" />
115                                         <Binding
116                                             Path="ExtentHeight"
117                                             RelativeSource="{RelativeSource TemplatedParent}" />
118                                         <Binding
119                                             Path="ViewportHeight"
120                                             RelativeSource="{RelativeSource TemplatedParent}" />
121                                     </MultiBinding>
122                                 </RepeatButton.Visibility>
123                                 <Path
124                                     Data="{StaticResource UpArrow}"
125                                     Fill="{StaticResource Menu.Static.Foreground}" />
126                             </RepeatButton>
127 
128                             <!-- 向下滾動按鈕 -->
129                             <RepeatButton
130                                 Grid.Row="2"
131                                 Grid.Column="0"
132                                 Command="{x:Static ScrollBar.LineDownCommand}"
133                                 CommandTarget="{Binding RelativeSource={RelativeSource TemplatedParent}}"
134                                 Focusable="false"
135                                 Style="{StaticResource MenuScrollButton}">
136                                 <RepeatButton.Visibility>
137                                     <MultiBinding
138                                         Converter="{StaticResource MenuScrollingVisibilityConverter}"
139                                         ConverterParameter="100"
140                                         FallbackValue="Visibility.Collapsed">
141                                         <Binding
142                                             Path="ComputedVerticalScrollBarVisibility"
143                                             RelativeSource="{RelativeSource TemplatedParent}" />
144                                         <Binding
145                                             Path="VerticalOffset"
146                                             RelativeSource="{RelativeSource TemplatedParent}" />
147                                         <Binding
148                                             Path="ExtentHeight"
149                                             RelativeSource="{RelativeSource TemplatedParent}" />
150                                         <Binding
151                                             Path="ViewportHeight"
152                                             RelativeSource="{RelativeSource TemplatedParent}" />
153                                     </MultiBinding>
154                                 </RepeatButton.Visibility>
155                                 <Path
156                                     Data="{StaticResource DownArrow}"
157                                     Fill="{StaticResource Menu.Static.Foreground}" />
158                             </RepeatButton>
159                         </Grid>
160                     </ControlTemplate>
161                 </Setter.Value>
162             </Setter>
163         </Style>
164 
165         <!-- Role 的檔案詳見 https://docs.microsoft.com/en-us/dotnet/api/system.windows.controls.menuitemrole?view=windowsdesktop-6.0 -->
166 
167         <!-- 當 Role= TopLevelItem 時的模版 -->
168         <ControlTemplate
169             x:Key="{ComponentResourceKey ResourceId=TopLevelItemTemplateKey,
170                                          TypeInTargetAssembly={x:Type MenuItem}}"
171             TargetType="{x:Type MenuItem}">
172             <Border
173                 x:Name="templateRoot"
174                 Background="{TemplateBinding Background}"
175                 BorderBrush="{TemplateBinding BorderBrush}"
176                 BorderThickness="{TemplateBinding BorderThickness}"
177                 SnapsToDevicePixels="true">
178                 <Grid VerticalAlignment="Center">
179                     <Grid.ColumnDefinitions>
180                         <ColumnDefinition Width="Auto" />
181                         <ColumnDefinition Width="Auto" />
182                     </Grid.ColumnDefinitions>
183                     <ContentPresenter
184                         x:Name="Icon"
185                         Width="16"
186                         Height="16"
187                         Margin="3"
188                         HorizontalAlignment="Center"
189                         VerticalAlignment="Center"
190                         ContentSource="Icon"
191                         SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
192                     <Path
193                         x:Name="GlyphPanel"
194                         Margin="3"
195                         VerticalAlignment="Center"
196                         Data="{StaticResource Checkmark}"
197                         Fill="{StaticResource Menu.Static.Foreground}"
198                         FlowDirection="LeftToRight"
199                         Visibility="Collapsed" />
200                     <ContentPresenter
201                         Grid.Column="1"
202                         Margin="{TemplateBinding Padding}"
203                         ContentSource="Header"
204                         RecognizesAccessKey="True"
205                         SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
206                 </Grid>
207             </Border>
208             <ControlTemplate.Triggers>
209                 <Trigger Property="Icon" Value="{x:Null}">
210                     <Setter TargetName="Icon" Property="Visibility" Value="Collapsed" />
211                 </Trigger>
212                 <Trigger Property="IsChecked" Value="true">
213                     <Setter TargetName="GlyphPanel" Property="Visibility" Value="Visible" />
214                     <Setter TargetName="Icon" Property="Visibility" Value="Collapsed" />
215                 </Trigger>
216                 <Trigger Property="IsHighlighted" Value="True">
217                     <Setter TargetName="templateRoot" Property="Background" Value="{StaticResource MenuItem.Highlight.Background}" />
218                     <Setter TargetName="templateRoot" Property="BorderBrush" Value="{StaticResource MenuItem.Highlight.Border}" />
219                 </Trigger>
220                 <Trigger Property="IsEnabled" Value="False">
221                     <Setter TargetName="templateRoot" Property="TextElement.Foreground" Value="{StaticResource Menu.Disabled.Foreground}" />
222                     <Setter TargetName="GlyphPanel" Property="Fill" Value="{StaticResource Menu.Disabled.Foreground}" />
223                 </Trigger>
224                 <MultiTrigger>
225                     <MultiTrigger.Conditions>
226                         <Condition Property="IsHighlighted" Value="True" />
227                         <Condition Property="IsEnabled" Value="False" />
228                     </MultiTrigger.Conditions>
229                     <Setter TargetName="templateRoot" Property="Background" Value="{StaticResource MenuItem.Highlight.Disabled.Background}" />
230                     <Setter TargetName="templateRoot" Property="BorderBrush" Value="{StaticResource MenuItem.Highlight.Disabled.Border}" />
231                 </MultiTrigger>
232             </ControlTemplate.Triggers>
233         </ControlTemplate>
234 
235         <!-- 當 Role= TopLevelHeader 時的模版 -->
236         <ControlTemplate
237             x:Key="{ComponentResourceKey ResourceId=TopLevelHeaderTemplateKey,
238                                          TypeInTargetAssembly={x:Type MenuItem}}"
239             TargetType="{x:Type MenuItem}">
240             <Border
241                 x:Name="templateRoot"
242                 Background="{TemplateBinding Background}"
243                 BorderBrush="{TemplateBinding BorderBrush}"
244                 BorderThickness="{TemplateBinding BorderThickness}"
245                 SnapsToDevicePixels="true">
246                 <Grid VerticalAlignment="Center">
247                     <Grid.ColumnDefinitions>
248                         <ColumnDefinition Width="Auto" />
249                         <ColumnDefinition Width="Auto" />
250                     </Grid.ColumnDefinitions>
251                     <ContentPresenter
252                         x:Name="Icon"
253                         Width="16"
254                         Height="16"
255                         Margin="3"
256                         HorizontalAlignment="Center"
257                         VerticalAlignment="Center"
258                         ContentSource="Icon"
259                         SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
260                     <Path
261                         x:Name="GlyphPanel"
262                         Margin="3"
263                         VerticalAlignment="Center"
264                         Data="{StaticResource Checkmark}"
265                         Fill="{TemplateBinding Foreground}"
266                         FlowDirection="LeftToRight"
267                         Visibility="Collapsed" />
268                     <ContentPresenter
269                         Grid.Column="1"
270                         Margin="{TemplateBinding Padding}"
271                         ContentSource="Header"
272                         RecognizesAccessKey="True"
273                         SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
274                     <Popup
275                         x:Name="PART_Popup"
276                         AllowsTransparency="true"
277                         Focusable="false"
278                         IsOpen="{Binding IsSubmenuOpen,
279                                          RelativeSource={RelativeSource TemplatedParent}}"
280                         Placement="Bottom"
281                         PlacementTarget="{Binding ElementName=templateRoot}"
282                         PopupAnimation="{DynamicResource {x:Static SystemParameters.MenuPopupAnimationKey}}">
283 
284                         <!-- 彈出的子選單 -->
285                         <Border
286                             x:Name="SubMenuBorder"
287                             Padding="2"
288                             Background="{StaticResource Menu.Static.Background}"
289                             BorderBrush="{StaticResource Menu.Static.Border}"
290                             BorderThickness="1">
291                             <ScrollViewer
292                                 x:Name="SubMenuScrollViewer"
293                                 Style="{DynamicResource {ComponentResourceKey ResourceId=MenuScrollViewer,
294                                                                               TypeInTargetAssembly={x:Type FrameworkElement}}}">
295                                 <Grid RenderOptions.ClearTypeHint="Enabled">
296                                     <Canvas
297                                         Width="0"
298                                         Height="0"
299                                         HorizontalAlignment="Left"
300                                         VerticalAlignment="Top">
301                                         <Rectangle
302                                             x:Name="OpaqueRect"
303                                             Width="{Binding ActualWidth,
304                                                             ElementName=SubMenuBorder}"
305                                             Height="{Binding ActualHeight,
306                                                              ElementName=SubMenuBorder}"
307                                             Fill="{Binding Background,
308                                                            ElementName=SubMenuBorder}" />
309                                     </Canvas>
310                                     <Rectangle
311                                         Width="1"
312                                         Margin="29,2,0,2"
313                                         HorizontalAlignment="Left"
314                                         Fill="{StaticResource Menu.Static.Separator}" />
315                                     <ItemsPresenter
316                                         x:Name="ItemsPresenter"
317                                         Grid.IsSharedSizeScope="true"
318                                         KeyboardNavigation.DirectionalNavigation="Cycle"
319                                         KeyboardNavigation.TabNavigation="Cycle"
320                                         SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
321                                 </Grid>
322                             </ScrollViewer>
323                         </Border>
324                     </Popup>
325                 </Grid>
326             </Border>
327             <ControlTemplate.Triggers>
328                 <Trigger Property="IsSuspendingPopupAnimation" Value="true">
329                     <Setter TargetName="PART_Popup" Property="PopupAnimation" Value="None" />
330                 </Trigger>
331                 <Trigger Property="Icon" Value="{x:Null}">
332                     <Setter TargetName="Icon" Property="Visibility" Value="Collapsed" />
333                 </Trigger>
334                 <Trigger Property="IsChecked" Value="true">
335                     <Setter TargetName="GlyphPanel" Property="Visibility" Value="Visible" />
336                     <Setter TargetName="Icon" Property="Visibility" Value="Collapsed" />
337                 </Trigger>
338                 <Trigger Property="IsHighlighted" Value="True">
339                     <Setter TargetName="templateRoot" Property="Background" Value="{StaticResource MenuItem.Highlight.Background}" />
340                     <Setter TargetName="templateRoot" Property="BorderBrush" Value="{StaticResource MenuItem.Highlight.Border}" />
341                 </Trigger>
342                 <Trigger Property="IsEnabled" Value="False">
343                     <Setter TargetName="templateRoot" Property="TextElement.Foreground" Value="{StaticResource Menu.Disabled.Foreground}" />
344                     <Setter TargetName="GlyphPanel" Property="Fill" Value="{StaticResource Menu.Disabled.Foreground}" />
345                 </Trigger>
346                 <Trigger SourceName="SubMenuScrollViewer" Property="ScrollViewer.CanContentScroll" Value="false">
347                     <Setter TargetName="OpaqueRect" Property="Canvas.Top" Value="{Binding VerticalOffset, ElementName=SubMenuScrollViewer}" />
348                     <Setter TargetName="OpaqueRect" Property="Canvas.Left" Value="{Binding HorizontalOffset, ElementName=SubMenuScrollViewer}" />
349                 </Trigger>
350             </ControlTemplate.Triggers>
351         </ControlTemplate>
352 
353         <!-- MenuItem 常規的模版 -->
354         <ControlTemplate
355             x:Key="{ComponentResourceKey ResourceId=SubmenuItemTemplateKey,
356                                          TypeInTargetAssembly={x:Type MenuItem}}"
357             TargetType="{x:Type MenuItem}">
358             <Border
359                 x:Name="templateRoot"
360                 Height="22"
361                 Background="{TemplateBinding Background}"
362                 BorderBrush="{TemplateBinding BorderBrush}"
363                 BorderThickness="{TemplateBinding BorderThickness}"
364                 SnapsToDevicePixels="true">
365                 <Grid Margin="-1">
366                     <!-- 布局 0~5 共 6 列, 其中 1,3,5 用作留空 -->
367                     <Grid.ColumnDefinitions>
368                         <ColumnDefinition
369                             Width="Auto"
370                             MinWidth="22"
371                             SharedSizeGroup="MenuItemIconColumnGroup" />
372                         <ColumnDefinition Width="13" />
373                         <ColumnDefinition Width="*" />
374                         <ColumnDefinition Width="30" />
375                         <ColumnDefinition
376                             Width="Auto"
377                             SharedSizeGroup="MenuItemIGTColumnGroup" />
378                         <ColumnDefinition Width="20" />
379                     </Grid.ColumnDefinitions>
380 
381                     <!-- 第 0 列 圖示 -->
382                     <ContentPresenter
383                         x:Name="Icon"
384                         Width="16"
385                         Height="16"
386                         Margin="3"
387                         HorizontalAlignment="Center"
388                         VerticalAlignment="Center"
389                         ContentSource="Icon"
390                         SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
391 
392                     <!-- 第 0 列,勾(用于標識是否勾選) -->
393                     <Border
394                         x:Name="GlyphPanel"
395                         Width="22"
396                         Height="22"
397                         Margin="-1,0,0,0"
398                         HorizontalAlignment="Center"
399                         VerticalAlignment="Center"
400                         Background="{StaticResource MenuItem.Selected.Background}"
401                         BorderBrush="{StaticResource MenuItem.Selected.Border}"
402                         BorderThickness="1"
403                         ClipToBounds="False"
404                         Visibility="Hidden">
405                         <Path
406                             x:Name="Glyph"
407                             Width="10"
408                             Height="11"
409                             Data="{StaticResource Checkmark}"
410                             Fill="{StaticResource Menu.Static.Foreground}"
411                             FlowDirection="LeftToRight" />
412                     </Border>
413 
414                     <!-- 第 2 列 MenuItem.Header 的內容 -->
415                     <ContentPresenter
416                         x:Name="menuHeaderContainer"
417                         Grid.Column="2"
418                         Margin="{TemplateBinding Padding}"
419                         HorizontalAlignment="Left"
420                         VerticalAlignment="Center"
421                         ContentSource="Header"
422                         RecognizesAccessKey="True"
423                         SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
424 
425                     <!-- 第 4 列 MenuItem.menuGestureText 的內容 -->
426                     <TextBlock
427                         x:Name="menuGestureText"
428                         Grid.Column="4"
429                         Margin="{TemplateBinding Padding}"
430                         VerticalAlignment="Center"
431                         Opacity="0.7"
432                         Text="{TemplateBinding InputGestureText}" />
433                 </Grid>
434             </Border>
435             <ControlTemplate.Triggers>
436                 <!-- 有圖示的時候顯示圖示,沒圖示則隱藏 -->
437                 <Trigger Property="Icon" Value="{x:Null}">
438                     <Setter TargetName="Icon" Property="Visibility" Value="Collapsed" />
439                 </Trigger>
440 
441                 <!-- 被勾選時顯示勾選,并且隱藏圖示 -->
442                 <Trigger Property="IsChecked" Value="True">
443                     <Setter TargetName="GlyphPanel" Property="Visibility" Value="Visible" />
444                     <Setter TargetName="Icon" Property="Visibility" Value="Collapsed" />
445                 </Trigger>
446 
447                 <!-- 高亮時 改變背景色和邊框色 -->
448                 <Trigger Property="IsHighlighted" Value="True">
449                     <Setter TargetName="templateRoot" Property="Background" Value="{StaticResource MenuItem.Highlight.Background}" />
450                     <Setter TargetName="templateRoot" Property="BorderBrush" Value="{StaticResource MenuItem.Highlight.Border}" />
451                 </Trigger>
452 
453                 <!-- 不可用時 將文字內容和勾設為禁用色 -->
454                 <Trigger Property="IsEnabled" Value="False">
455                     <Setter TargetName="templateRoot" Property="TextElement.Foreground" Value="{StaticResource Menu.Disabled.Foreground}" />
456                     <Setter TargetName="Glyph" Property="Fill" Value="{StaticResource Menu.Disabled.Foreground}" />
457                 </Trigger>
458 
459                 <!-- 高亮且禁用時  改變背景色和邊框色 -->
460                 <MultiTrigger>
461                     <MultiTrigger.Conditions>
462                         <Condition Property="IsHighlighted" Value="True" />
463                         <Condition Property="IsEnabled" Value="False" />
464                     </MultiTrigger.Conditions>
465                     <Setter TargetName="templateRoot" Property="Background" Value="{StaticResource MenuItem.Highlight.Disabled.Background}" />
466                     <Setter TargetName="templateRoot" Property="BorderBrush" Value="{StaticResource MenuItem.Highlight.Disabled.Border}" />
467                 </MultiTrigger>
468             </ControlTemplate.Triggers>
469         </ControlTemplate>
470 
471         <!-- 當 Role= SubmenuHeader 即有子選單時的模版 -->
472         <!-- 與常規模版的主要區別是增加了子選單的 popup 和彈出影片等 -->
473         <ControlTemplate
474             x:Key="{ComponentResourceKey ResourceId=SubmenuHeaderTemplateKey,
475                                          TypeInTargetAssembly={x:Type MenuItem}}"
476             TargetType="{x:Type MenuItem}">
477             <Border
478                 x:Name="templateRoot"
479                 Height="22"
480                 Background="{TemplateBinding Background}"
481                 BorderBrush="{TemplateBinding BorderBrush}"
482                 BorderThickness="{TemplateBinding BorderThickness}"
483                 SnapsToDevicePixels="true">
484                 <Grid Margin="-1">
485                     <Grid.ColumnDefinitions>
486                         <ColumnDefinition
487                             Width="Auto"
488                             MinWidth="22"
489                             SharedSizeGroup="MenuItemIconColumnGroup" />
490                         <ColumnDefinition Width="13" />
491                         <ColumnDefinition Width="*" />
492                         <ColumnDefinition Width="30" />
493                         <ColumnDefinition
494                             Width="Auto"
495                             SharedSizeGroup="MenuItemIGTColumnGroup" />
496                         <ColumnDefinition Width="20" />
497                     </Grid.ColumnDefinitions>
498                     <ContentPresenter
499                         x:Name="Icon"
500                         Width="16"
501                         Height="16"
502                         Margin="3"
503                         HorizontalAlignment="Center"
504                         VerticalAlignment="Center"
505                         ContentSource="Icon"
506                         SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
507                     <Border
508                         x:Name="GlyphPanel"
509                         Width="22"
510                         Height="22"
511                         Margin="-1,0,0,0"
512                         VerticalAlignment="Center"
513                         Background="{StaticResource MenuItem.Highlight.Background}"
514                         BorderBrush="{StaticResource MenuItem.Highlight.Border}"
515                         BorderThickness="1"
516                         Visibility="Hidden">
517                         <Path
518                             x:Name="Glyph"
519                             Width="9"
520                             Height="11"
521                             Data="{DynamicResource Checkmark}"
522                             Fill="{StaticResource Menu.Static.Foreground}"
523                             FlowDirection="LeftToRight" />
524                     </Border>
525                     <ContentPresenter
526                         Grid.Column="2"
527                         Margin="{TemplateBinding Padding}"
528                         HorizontalAlignment="Left"
529                         VerticalAlignment="Center"
530                         ContentSource="Header"
531                         RecognizesAccessKey="True"
532                         SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
533                     <TextBlock
534                         Grid.Column="4"
535                         Margin="{TemplateBinding Padding}"
536                         VerticalAlignment="Center"
537                         Opacity="0.7"
538                         Text="{TemplateBinding InputGestureText}" />
539                     <Path
540                         x:Name="RightArrow"
541                         Grid.Column="5"
542                         Margin="10,0,0,0"
543                         HorizontalAlignment="Left"
544                         VerticalAlignment="Center"
545                         Data="{StaticResource RightArrow}"
546                         Fill="{StaticResource Menu.Static.Foreground}" />
547                     <Popup
548                         x:Name="PART_Popup"
549                         AllowsTransparency="true"
550                         Focusable="false"
551                         HorizontalOffset="-2"
552                         IsOpen="{Binding IsSubmenuOpen,
553                                          RelativeSource={RelativeSource TemplatedParent}}"
554                         Placement="Right"
555                         PopupAnimation="{DynamicResource {x:Static SystemParameters.MenuPopupAnimationKey}}"
556                         VerticalOffset="-3">
557 
558                         <Border
559                             x:Name="SubMenuBorder"
560                             Padding="2"
561                             Background="{StaticResource Menu.Static.Background}"
562                             BorderBrush="{StaticResource Menu.Static.Border}"
563                             BorderThickness="1">
564                             <ScrollViewer
565                                 x:Name="SubMenuScrollViewer"
566                                 Style="{DynamicResource {ComponentResourceKey ResourceId=MenuScrollViewer,
567                                                                               TypeInTargetAssembly={x:Type FrameworkElement}}}">
568                                 <Grid RenderOptions.ClearTypeHint="Enabled">
569                                     <Canvas
570                                         Width="0"
571                                         Height="0"
572                                         HorizontalAlignment="Left"
573                                         VerticalAlignment="Top">
574                                         <Rectangle
575                                             x:Name="OpaqueRect"
576                                             Width="{Binding ActualWidth,
577                                                             ElementName=SubMenuBorder}"
578                                             Height="{Binding ActualHeight,
579                                                              ElementName=SubMenuBorder}"
580                                             Fill="{Binding Background,
581                                                            ElementName=SubMenuBorder}" />
582                                     </Canvas>
583                                     <Rectangle
584                                         Width="1"
585                                         Margin="29,2,0,2"
586                                         HorizontalAlignment="Left"
587                                         Fill="{DynamicResource {x:Static SystemColors.ControlDarkBrushKey}}" />
588                                     <ItemsPresenter
589                                         x:Name="ItemsPresenter"
590                                         Grid.IsSharedSizeScope="true"
591                                         KeyboardNavigation.DirectionalNavigation="Cycle"
592                                         KeyboardNavigation.TabNavigation="Cycle"
593                                         SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
594                                 </Grid>
595                             </ScrollViewer>
596                         </Border>
597                     </Popup>
598                 </Grid>
599             </Border>
600             <ControlTemplate.Triggers>
601                 <Trigger Property="IsSuspendingPopupAnimation" Value="true">
602                     <Setter TargetName="PART_Popup" Property="PopupAnimation" Value="None" />
603                 </Trigger>
604                 <Trigger Property="Icon" Value="{x:Null}">
605                     <Setter TargetName="Icon" Property="Visibility" Value="Collapsed" />
606                 </Trigger>
607                 <Trigger Property="IsChecked" Value="True">
608                     <Setter TargetName="GlyphPanel" Property="Visibility" Value="Visible" />
609                     <Setter TargetName="Icon" Property="Visibility" Value="Collapsed" />
610                 </Trigger>
611                 <Trigger Property="IsHighlighted" Value="True">
612                     <Setter TargetName="templateRoot" Property="Background" Value="Transparent" />
613                     <Setter TargetName="templateRoot" Property="BorderBrush" Value="{StaticResource MenuItem.Highlight.Border}" />
614                 </Trigger>
615                 <Trigger Property="IsEnabled" Value="False">
616                     <Setter TargetName="templateRoot" Property="TextElement.Foreground" Value="{StaticResource Menu.Disabled.Foreground}" />
617                     <Setter TargetName="Glyph" Property="Fill" Value="{StaticResource Menu.Disabled.Foreground}" />
618                     <Setter TargetName="RightArrow" Property="Fill" Value="{StaticResource Menu.Disabled.Foreground}" />
619                 </Trigger>
620                 <Trigger SourceName="SubMenuScrollViewer" Property="ScrollViewer.CanContentScroll" Value="false">
621                     <Setter TargetName="OpaqueRect" Property="Canvas.Top" Value="{Binding VerticalOffset, ElementName=SubMenuScrollViewer}" />
622                     <Setter TargetName="OpaqueRect" Property="Canvas.Left" Value="{Binding HorizontalOffset, ElementName=SubMenuScrollViewer}" />
623                 </Trigger>
624             </ControlTemplate.Triggers>
625         </ControlTemplate>
626 
627         <Style
628             x:Key="MenuItemStyle1"
629             TargetType="{x:Type MenuItem}">
630             <Setter Property="HorizontalContentAlignment" Value="{Binding HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}" />
631             <Setter Property="VerticalContentAlignment" Value="{Binding VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}" />
632             <Setter Property="Background" Value="Transparent" />
633             <Setter Property="BorderBrush" Value="Transparent" />
634             <Setter Property="BorderThickness" Value="1" />
635             <Setter Property="ScrollViewer.PanningMode" Value="Both" />
636             <Setter Property="Stylus.IsFlicksEnabled" Value="False" />
637             <!-- MenuItem 的常規版 -->
638             <Setter Property="Template" Value="{DynamicResource {ComponentResourceKey ResourceId=SubmenuItemTemplateKey, TypeInTargetAssembly={x:Type MenuItem}}}" />
639 
640             <Style.Triggers>
641                 <Trigger Property="Role" Value="TopLevelHeader">
642                     <Setter Property="Background" Value="Transparent" />
643                     <Setter Property="BorderBrush" Value="Transparent" />
644                     <Setter Property="Foreground" Value="{StaticResource Menu.Static.Foreground}" />
645                     <Setter Property="Template" Value="{DynamicResource {ComponentResourceKey ResourceId=TopLevelHeaderTemplateKey, TypeInTargetAssembly={x:Type MenuItem}}}" />
646                     <Setter Property="Padding" Value="6,0" />
647                 </Trigger>
648                 <Trigger Property="Role" Value="TopLevelItem">
649                     <Setter Property="Background" Value="{StaticResource Menu.Static.Background}" />
650                     <Setter Property="BorderBrush" Value="{StaticResource Menu.Static.Border}" />
651                     <Setter Property="Foreground" Value="{StaticResource Menu.Static.Foreground}" />
652                     <Setter Property="Template" Value="{DynamicResource {ComponentResourceKey ResourceId=TopLevelItemTemplateKey, TypeInTargetAssembly={x:Type MenuItem}}}" />
653                     <Setter Property="Padding" Value="6,0" />
654                 </Trigger>
655 
656                 <!-- 當 Role= SubmenuHeader 時 改變模版為 SubmenuHeaderTemplateKey -->
657                 <Trigger Property="Role" Value="SubmenuHeader">
658                     <Setter Property="Template" Value="{DynamicResource {ComponentResourceKey ResourceId=SubmenuHeaderTemplateKey, TypeInTargetAssembly={x:Type MenuItem}}}" />
659                 </Trigger>
660             </Style.Triggers>
661         </Style>

 

轉載請註明出處,本文鏈接:https://www.uj5u.com/net/459472.html

標籤:.NET技术

上一篇:ASP.NET Core + Docker +Jenkins 實作持續集成

下一篇:wpf MenuItem 默認樣式存檔,方便手頭沒有 vs 時查閱.

標籤雲
其他(157675) Python(38076) JavaScript(25376) Java(17977) C(15215) 區塊鏈(8255) C#(7972) AI(7469) 爪哇(7425) MySQL(7132) html(6777) 基礎類(6313) sql(6102) 熊猫(6058) PHP(5869) 数组(5741) R(5409) Linux(5327) 反应(5209) 腳本語言(PerlPython)(5129) 非技術區(4971) Android(4554) 数据框(4311) css(4259) 节点.js(4032) C語言(3288) json(3245) 列表(3129) 扑(3119) C++語言(3117) 安卓(2998) 打字稿(2995) VBA(2789) Java相關(2746) 疑難問題(2699) 细绳(2522) 單片機工控(2479) iOS(2429) ASP.NET(2402) MongoDB(2323) 麻木的(2285) 正则表达式(2254) 字典(2211) 循环(2198) 迅速(2185) 擅长(2169) 镖(2155) 功能(1967) .NET技术(1958) Web開發(1951) python-3.x(1918) HtmlCss(1915) 弹簧靴(1913) C++(1909) xml(1889) PostgreSQL(1872) .NETCore(1853) 谷歌表格(1846) Unity3D(1843) for循环(1842)

熱門瀏覽
  • WebAPI簡介

    Web體系結構: 有三個核心:資源(resource),URL(統一資源識別符號)和表示 他們的關系是這樣的:一個資源由一個URL進行標識,HTTP客戶端使用URL定位資源,表示是從資源回傳資料,媒體型別是資源回傳的資料格式。 接下來我們說下HTTP. HTTP協議的系統是一種無狀態的方式,使用請求/ ......

    uj5u.com 2020-09-09 22:07:47 more
  • asp.net core 3.1 入口:Program.cs中的Main函式

    本文分析Program.cs 中Main()函式中代碼的運行順序分析asp.net core程式的啟動,重點不是剖析原始碼,而是理清程式開始時執行的順序。到呼叫了哪些實體,哪些法方。asp.net core 3.1 的程式入口在專案Program.cs檔案里,如下。ususing System; us ......

    uj5u.com 2020-09-09 22:07:49 more
  • asp.net網站作為websocket服務端的應用該如何寫

    最近被websocket的一個問題困擾了很久,有一個需求是在web網站中搭建websocket服務。客戶端通過網頁與服務器建立連接,然后服務器根據ip給客戶端網頁發送資訊。 其實,這個需求并不難,只是剛開始對websocket的內容不太了解。上網搜索了一下,有通過asp.net core 實作的、有 ......

    uj5u.com 2020-09-09 22:08:02 more
  • ASP.NET 開源匯入匯出庫Magicodes.IE Docker中使用

    Magicodes.IE在Docker中使用 更新歷史 2019.02.13 【Nuget】版本更新到2.0.2 【匯入】修復單列匯入的Bug,單元測驗“OneColumnImporter_Test”。問題見(https://github.com/dotnetcore/Magicodes.IE/is ......

    uj5u.com 2020-09-09 22:08:05 more
  • 在webform中使用ajax

    如果你用過Asp.net webform, 說明你也算是.NET 開發的老兵了。WEBform應該是2011 2013左右,當時還用visual studio 2005、 visual studio 2008。后來基本都用的是MVC。 如果是新開發的專案,估計沒人會用webform技術。但是有些舊版 ......

    uj5u.com 2020-09-09 22:08:50 more
  • iis添加asp.net網站,訪問提示:由于擴展配置問題而無法提供您請求的

    今天在iis服務器配置asp.net網站,遇到一個問題,記錄一下: 問題:由于擴展配置問題而無法提供您請求的頁面。如果該頁面是腳本,請添加處理程式。如果應下載檔案,請添加 MIME 映射。 WindowServer2012服務器,添加角色安裝完.netframework和iis之后,運行aspx頁面 ......

    uj5u.com 2020-09-09 22:10:00 more
  • WebAPI-處理架構

    帶著問題去思考,大家好! 問題1:HTTP請求和回傳相應的HTTP回應資訊之間發生了什么? 1:首先是最底層,托管層,位于WebAPI和底層HTTP堆疊之間 2:其次是 訊息處理程式管道層,這里比如日志和快取。OWIN的參考是將訊息處理程式管道的一些功能下移到堆疊下端的OWIN中間件了。 3:控制器處理 ......

    uj5u.com 2020-09-09 22:11:13 more
  • 微信門戶開發框架-使用指導說明書

    微信門戶應用管理系統,采用基于 MVC + Bootstrap + Ajax + Enterprise Library的技術路線,界面層采用Boostrap + Metronic組合的前端框架,資料訪問層支持Oracle、SQLServer、MySQL、PostgreSQL等資料庫。框架以MVC5,... ......

    uj5u.com 2020-09-09 22:15:18 more
  • WebAPI-HTTP編程模型

    帶著問題去思考,大家好!它是什么?它包含什么?它能干什么? 訊息 HTTP編程模型的核心就是訊息抽象,表示為:HttPRequestMessage,HttpResponseMessage.用于客戶端和服務端之間交換請求和回應訊息。 HttpMethod類包含了一組靜態屬性: private stat ......

    uj5u.com 2020-09-09 22:15:23 more
  • 部署WebApi隨筆

    一、跨域 NuGet參考Microsoft.AspNet.WebApi.Cors WebApiConfig.cs中配置: // Web API 配置和服務 config.EnableCors(new EnableCorsAttribute("*", "*", "*")); 二、清除默認回傳XML格式 ......

    uj5u.com 2020-09-09 22:15:48 more
最新发布
  • C#多執行緒學習(二) 如何操縱一個執行緒

    <a href="https://www.cnblogs.com/x-zhi/" target="_blank"><img width="48" height="48" class="pfs" src="https://pic.cnblogs.com/face/2943582/20220801082530.png" alt="" /></...

    uj5u.com 2023-04-19 09:17:20 more
  • C#多執行緒學習(二) 如何操縱一個執行緒

    C#多執行緒學習(二) 如何操縱一個執行緒 執行緒學習第一篇:C#多執行緒學習(一) 多執行緒的相關概念 下面我們就動手來創建一個執行緒,使用Thread類創建執行緒時,只需提供執行緒入口即可。(執行緒入口使程式知道該讓這個執行緒干什么事) 在C#中,執行緒入口是通過ThreadStart代理(delegate)來提供的 ......

    uj5u.com 2023-04-19 09:16:49 more
  • 記一次 .NET某醫療器械清洗系統 卡死分析

    <a href="https://www.cnblogs.com/huangxincheng/" target="_blank"><img width="48" height="48" class="pfs" src="https://pic.cnblogs.com/face/214741/20200614104537.png" alt="" /&g...

    uj5u.com 2023-04-18 08:39:04 more
  • 記一次 .NET某醫療器械清洗系統 卡死分析

    一:背景 1. 講故事 前段時間協助訓練營里的一位朋友分析了一個程式卡死的問題,回過頭來看這個案例比較經典,這篇稍微整理一下供后來者少踩坑吧。 二:WinDbg 分析 1. 為什么會卡死 因為是表單程式,理所當然就是看主執行緒此時正在做什么? 可以用 ~0s ; k 看一下便知。 0:000> k # ......

    uj5u.com 2023-04-18 08:33:10 more
  • SignalR, No Connection with that ID,IIS

    <a href="https://www.cnblogs.com/smartstar/" target="_blank"><img width="48" height="48" class="pfs" src="https://pic.cnblogs.com/face/u36196.jpg" alt="" /></a>...

    uj5u.com 2023-03-30 17:21:52 more
  • 一次對pool的誤用導致的.net頻繁gc的診斷分析

    <a href="https://www.cnblogs.com/dotnet-diagnostic/" target="_blank"><img width="48" height="48" class="pfs" src="https://pic.cnblogs.com/face/3115652/20230225090434.png" alt=""...

    uj5u.com 2023-03-28 10:15:33 more
  • 一次對pool的誤用導致的.net頻繁gc的診斷分析

    <a href="https://www.cnblogs.com/dotnet-diagnostic/" target="_blank"><img width="48" height="48" class="pfs" src="https://pic.cnblogs.com/face/3115652/20230225090434.png" alt=""...

    uj5u.com 2023-03-28 10:13:31 more
  • C#遍歷指定檔案夾中所有檔案的3種方法

    <a href="https://www.cnblogs.com/xbhp/" target="_blank"><img width="48" height="48" class="pfs" src="https://pic.cnblogs.com/face/957602/20230310105611.png" alt="" /></a&...

    uj5u.com 2023-03-27 14:46:55 more
  • C#/VB.NET:如何將PDF轉為PDF/A

    <a href="https://www.cnblogs.com/Carina-baby/" target="_blank"><img width="48" height="48" class="pfs" src="https://pic.cnblogs.com/face/2859233/20220427162558.png" alt="" />...

    uj5u.com 2023-03-27 14:46:35 more
  • 武裝你的WEBAPI-OData聚合查詢

    <a href="https://www.cnblogs.com/podolski/" target="_blank"><img width="48" height="48" class="pfs" src="https://pic.cnblogs.com/face/616093/20140323000327.png" alt="" /><...

    uj5u.com 2023-03-27 14:46:16 more