马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
书接上回,我们的Fluent WPF的版图已经完成了:
先来看看效果图(win11):
有以下xaml代码:- <TextBlock.ContextMenu>
-
-
- <ContextMenu>
-
-
-
-
- <MenuItem Header="Menu Item 1" Icon="Cd" InputGestureText="aa?" />
-
-
-
-
- <MenuItem Header="Menu Item 2" >
-
-
-
-
-
-
- <MenuItem Header="Child Item 1" Icon="Ab" />
-
-
-
-
-
-
- <MenuItem Header="Child Item 2" Icon="Ad" />
-
-
-
-
-
-
- <MenuItem Header="Child Item 3" IsCheckable="True" IsChecked="True"/>
-
-
-
-
- </MenuItem>
-
-
-
-
- <MenuItem Header="Menu Item 3" Icon="cd" />
-
-
- </ContextMenu>
- </TextBlock.ContextMenu>
复制代码
(由于我的win10捏造机坏了,临时没有测试)
前面的工作已经办理了让恣意窗口支持Acrylic材质的题目,本文重点先容ContentMenu和MenuItem的样式适配和实现。
本文的Demo:
TwilightLemon/WindowEffectTest: 测试win10/11的含糊效果 (github.com)
一、为什么必要一个新的ContextMenu和MenuItem模板
如果你直接给ContextMenu应用WindowMaterial殊效,大概会出现以下貌寝的效果:
大概:
缘故因由在于古老的ContextMenu和MenuItem模板和样式并不能通过简朴修改Background实现我们想要的结构和交互效果。
二、ContextMenu与MenuItem的结构
1. ContextMenu 的结构
ContextMenu直观上看是一个Popup,但它的控件模板并不包罗Popup,而是直接指定内部元素(包罗一个ScrollViewer和StackPanel)。因此不能从控件模板中更换Popup为自界说的FluentPopup,必要利用其他本领让其内部Popup也支持Acrylic材质(见下文)。
2. MenuItem 的四种形态
在示例代码堆栈中展示了较为完备的Menu干系的结构:
MenuItem根据其在菜单树中的位置,通过Role属性分为四种形态。我们在Style.Triggers中分别为它们指定了差异的模板:
- TopLevelHeader: 顶级菜单项,且包罗子菜单Popup(比方菜单栏上的”File”)。
- TopLevelItem: 顶级菜单项,不含子菜单(比方菜单栏上的”Help”)。
- SubmenuHeader: 子菜单项,且包罗下一级子菜单Popup。
- SubmenuItem: 子菜单项,不含子菜单(叶子节点)。其模板重要处理惩罚图标、笔墨、快捷键的结构。
三、重写Menu干系控件模板和样式
1. ContextMenu 样式
ContextMenu的样式重要参考了.NET 9自带的Fluent样式,并作了一些调解以适配Acrylic材质。重要目的是覆盖原始模板的Icon部门白框和分割线:
由于我们的WindowMaterial已经为窗口主动附加上圆角、阴影和亚克力材质的DWM效果,以是我们只必要将ContextMenu的配景设置为透明,并移除不须要的边框和分割线即可。
别的,还添加了弹出动画(是在Popup内部做的,并非对window,效果大概不会很理想)。
2. MenuItem 样式
MenuItem的样式重要处理惩罚了图标、笔墨、快捷键的结构,并根据差异的脚色(TopLevelHeader、TopLevelItem、SubmenuHeader、SubmenuItem)应用差异的模板。
- TopLevelHeader: 只包罗Icon和Header,以及弹出的Popup,只必要更换为自界说的FluentPopup即可。
- TopLevelItem: 只包罗Icon和Header,无Popup。
- SubmenuHeader: 包罗Icon、Header和Chevron图标(这里就是一个睁开的箭头图标,但是官方叫做雪佛龙..?),以及弹出的Popup,同样更换为FluentPopup。
- SubmenuItem: 叶子节点,包罗Icon、Header和InputGestureText。 其模板重要处理惩罚图标、笔墨、快捷键的结构。
菜单项重要分为三个部门:Icon图标、Header笔墨和最右侧的提示笔墨或睁开箭头图标。只必要保持三个部门的结构对其即可。如果IsCheckable为True,则Icon部门被自界说图标占据(√, 当IsChecked为True时)。

以下是完备的资源字典,包罗了完备的表明。
  -
- 1 <ResourceDictionary
-
- 2
-
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
-
- 3
-
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
-
- 4
-
- xmlns:local="clr-namespace:WindowEffectTest"
-
- 5
-
- xmlns:sys="clr-namespace:System;assembly=mscorlib">
-
- 6
-
- 7
-
-
-
- 8
-
- <Thickness x:Key="MenuBorderPadding">0,3,0,3</Thickness>
-
- 9
- 10
-
-
- 11
-
- <Thickness x:Key="MenuItemMargin">4,1</Thickness>
- 12
- 13
-
-
- 14
-
- <Thickness x:Key="MenuItemContentPadding">10,6</Thickness>
- 15
- 16
-
-
- 17
-
- <Thickness x:Key="TopLevelItemMargin">4</Thickness>
- 18
- 19
-
-
- 20
-
- <Thickness x:Key="TopLevelContentMargin">10</Thickness>
- 21
- 22
-
-
- 23
-
- <CornerRadius x:Key="MenuCornerRadius">4</CornerRadius>
- 24
- 25
-
-
- 26
-
- <CornerRadius x:Key="TopLevelCornerRadius">6</CornerRadius>
- 27
- 28
-
-
- 29
-
- <Duration x:Key="MenuAnimationDuration">0:0:0.167</Duration>
- 30
- 31
-
-
- 32
-
- <PathGeometry x:Key="CheckGraph">
- 33
-
-
-
- M392.533333 806.4L85.333333 503.466667l59.733334-59.733334 247.466666 247.466667L866.133333 213.333333l59.733334 59.733334L392.533333 806.4z
- 34
-
- </PathGeometry>
- 35
- 36
-
-
- 37
-
- <PathGeometry x:Key="ForwardGraph">
- 38
-
-
-
- M283.648 174.081l57.225-59.008 399.479 396.929-399.476 396.924-57.228-59.004 335.872-337.92z
- 39
-
- </PathGeometry>
- 40
- 41
-
-
- 42
-
-
- 49
- 50
-
-
- 51
-
-
- 66
- 67
-
-
- 68
-
-
- 125
- 126
-
-
- 127
-
- <ControlTemplate x:Key="{x:Static MenuItem.TopLevelHeaderTemplateKey}" TargetType="{x:Type MenuItem}">
- 128
-
-
-
- <Border
- 129
-
-
-
-
-
- x:Name="Border"
- 130
-
-
-
-
-
- Margin="{StaticResource TopLevelItemMargin}"
- 131
-
-
-
-
-
- Background="{TemplateBinding Background}"
- 132
-
-
-
-
-
- BorderBrush="{TemplateBinding BorderBrush}"
- 133
-
-
-
-
-
- BorderThickness="{TemplateBinding BorderThickness}"
- 134
-
-
-
-
-
- CornerRadius="{StaticResource TopLevelCornerRadius}">
- 135
-
-
-
-
-
- <Grid>
- 136
-
-
-
-
-
-
-
- <Grid.RowDefinitions>
- 137
-
-
-
-
-
-
-
-
-
- <RowDefinition Height="*" />
- 138
-
-
-
-
-
-
-
-
-
- <RowDefinition Height="Auto" />
- 139
-
-
-
-
-
-
-
- </Grid.RowDefinitions>
- 140
- 141
-
-
-
-
-
-
-
-
- 142
-
-
-
-
-
-
-
- <Grid Margin="{StaticResource TopLevelContentMargin}">
- 143
-
-
-
-
-
-
-
-
-
- <Grid.ColumnDefinitions>
- 144
-
-
-
-
-
-
-
-
-
-
-
- <ColumnDefinition Width="Auto" />
- 145
-
-
-
-
-
-
-
-
-
-
-
- <ColumnDefinition Width="*" />
- 146
-
-
-
-
-
-
-
-
-
- </Grid.ColumnDefinitions>
- 147
- 148
-
-
-
-
-
-
-
-
-
-
- 149
-
-
-
-
-
-
-
-
-
- <ContentPresenter
- 150
-
-
-
-
-
-
-
-
-
-
-
- x:Name="Icon"
- 151
-
-
-
-
-
-
-
-
-
-
-
- Grid.Column="0"
- 152
-
-
-
-
-
-
-
-
-
-
-
- Margin="0,0,6,0"
- 153
-
-
-
-
-
-
-
-
-
-
-
- VerticalAlignment="Center"
- 154
-
-
-
-
-
-
-
-
-
-
-
- Content="{TemplateBinding Icon}"
- 155
-
-
-
-
-
-
-
-
-
-
-
- SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
- 156
- 157
-
-
-
-
-
-
-
-
-
-
- 158
-
-
-
-
-
-
-
-
-
- <ContentPresenter
- 159
-
-
-
-
-
-
-
-
-
-
-
- x:Name="HeaderPresenter"
- 160
-
-
-
-
-
-
-
-
-
-
-
- Grid.Column="1"
- 161
-
-
-
-
-
-
-
-
-
-
-
- Margin="{TemplateBinding Padding}"
- 162
-
-
-
-
-
-
-
-
-
-
-
- VerticalAlignment="Center"
- 163
-
-
-
-
-
-
-
-
-
-
-
- ContentSource="Header"
- 164
-
-
-
-
-
-
-
-
-
-
-
- RecognizesAccessKey="True"
- 165
-
-
-
-
-
-
-
-
-
-
-
- SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
- 166
-
-
-
-
-
-
-
-
-
-
-
- TextElement.Foreground="{TemplateBinding Foreground}" />
- 167
-
-
-
-
-
-
-
- </Grid>
- 168
- 169
-
-
-
-
-
-
-
-
- 170
-
-
-
-
-
-
-
- <local:FluentPopup
- 171
-
-
-
-
-
-
-
-
-
- x:Name="PART_Popup"
- 172
-
-
-
-
-
-
-
-
-
- Grid.Row="1"
- 173
-
-
-
-
-
-
-
-
-
- Grid.Column="0"
- 174
-
-
-
-
-
-
-
-
-
- Focusable="False"
- 175
-
-
-
-
-
-
-
-
-
- HorizontalOffset="-12"
- 176
-
-
-
-
-
-
-
-
-
- IsOpen="{TemplateBinding IsSubmenuOpen}"
- 177
-
-
-
-
-
-
-
-
-
- Placement="Bottom"
- 178
-
-
-
-
-
-
-
-
-
- PlacementTarget="{Binding ElementName=Border}"
- 179
-
-
-
-
-
-
-
-
-
- PopupAnimation="None"
- 180
-
-
-
-
-
-
-
-
-
- VerticalOffset="1">
- 181
-
-
-
-
-
-
-
-
-
- <Grid
- 182
-
-
-
-
-
-
-
-
-
-
-
- x:Name="SubmenuBorder"
- 183
-
-
-
-
-
-
-
-
-
-
-
- Background="{DynamicResource PopupWindowBackground}"
- 184
-
-
-
-
-
-
-
-
-
-
-
- SnapsToDevicePixels="True">
- 185
-
-
-
-
-
-
-
-
-
-
-
-
- 186
-
-
-
-
-
-
-
-
-
-
-
- <Grid.RenderTransform>
- 187
-
-
-
-
-
-
-
-
-
-
-
-
-
- <TranslateTransform />
- 188
-
-
-
-
-
-
-
-
-
-
-
- </Grid.RenderTransform>
- 189
-
-
-
-
-
-
-
-
-
-
-
- <ScrollViewer
- 190
-
-
-
-
-
-
-
-
-
-
-
-
-
- Padding="{StaticResource MenuBorderPadding}"
- 191
-
-
-
-
-
-
-
-
-
-
-
-
-
- CanContentScroll="True"
- 192
-
-
-
-
-
-
-
-
-
-
-
-
-
- Style="{StaticResource MenuItemScrollViewerStyle}">
- 193
-
-
-
-
-
-
-
-
-
-
-
-
-
- <Grid>
- 194
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- 195
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- <ItemsPresenter
- 196
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- x:Name="ItemsPresenter"
- 197
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- Grid.IsSharedSizeScope="True"
- 198
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- KeyboardNavigation.DirectionalNavigation="Cycle"
- 199
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- KeyboardNavigation.TabNavigation="Cycle"
- 200
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
- 201
-
-
-
-
-
-
-
-
-
-
-
-
-
- </Grid>
- 202
-
-
-
-
-
-
-
-
-
-
-
- </ScrollViewer>
- 203
-
-
-
-
-
-
-
-
-
- </Grid>
- 204
-
-
-
-
-
-
-
- </local:FluentPopup>
- 205
-
-
-
-
-
- </Grid>
- 206
-
-
-
- </Border>
- 207
-
-
-
- <ControlTemplate.Triggers>
- 208
-
-
-
-
-
-
- 209
-
-
-
-
-
- <Trigger Property="Icon" Value="{x:Null}">
- 210
-
-
-
-
-
-
-
- <Setter TargetName="Icon" Property="Visibility" Value="Collapsed" />
- 211
-
-
-
-
-
- </Trigger>
- 212
-
-
-
-
-
-
- 213
-
-
-
-
-
- <Trigger Property="Header" Value="{x:Null}">
- 214
-
-
-
-
-
-
-
- <Setter TargetName="Icon" Property="Margin" Value="0" />
- 215
-
-
-
-
-
-
-
- <Setter TargetName="HeaderPresenter" Property="Visibility" Value="Collapsed" />
- 216
-
-
-
-
-
- </Trigger>
- 217
-
-
-
-
-
-
- 218
-
-
-
-
-
- <Trigger Property="IsHighlighted" Value="True">
- 219
-
-
-
-
-
-
-
- <Setter TargetName="Border" Property="Background" Value="{DynamicResource MaskColor}" />
- 220
-
-
-
-
-
- </Trigger>
- 221
-
-
-
-
-
-
- 222
-
-
-
-
-
- <Trigger Property="IsSubmenuOpen" Value="True">
- 223
-
-
-
-
-
-
-
- <Trigger.EnterActions>
- 224
-
-
-
-
-
-
-
-
-
- <BeginStoryboard>
- 225
-
-
-
-
-
-
-
-
-
-
-
- <Storyboard>
- 226
-
-
-
-
-
-
-
-
-
-
-
-
-
- <DoubleAnimation
- 227
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- Storyboard.TargetName="SubmenuBorder"
- 228
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- Storyboard.TargetProperty="(Border.RenderTransform).(TranslateTransform.Y)"
- 229
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- From="-45"
- 230
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- To="0"
- 231
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- Duration="{StaticResource MenuAnimationDuration}">
- 232
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- <DoubleAnimation.EasingFunction>
- 233
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- <CircleEase EasingMode="EaseOut" />
- 234
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- </DoubleAnimation.EasingFunction>
- 235
-
-
-
-
-
-
-
-
-
-
-
-
-
- </DoubleAnimation>
- 236
-
-
-
-
-
-
-
-
-
-
-
- </Storyboard>
- 237
-
-
-
-
-
-
-
-
-
- </BeginStoryboard>
- 238
-
-
-
-
-
-
-
- </Trigger.EnterActions>
- 239
-
-
-
-
-
- </Trigger>
- 240
-
-
-
- </ControlTemplate.Triggers>
- 241
-
- </ControlTemplate>
- 242
- 243
-
-
- 244
-
- <ControlTemplate x:Key="{x:Static MenuItem.TopLevelItemTemplateKey}" TargetType="{x:Type MenuItem}">
- 245
-
-
-
- <Border
- 246
-
-
-
-
-
- x:Name="Border"
- 247
-
-
-
-
-
- Margin="{StaticResource TopLevelItemMargin}"
- 248
-
-
-
-
-
- Background="{TemplateBinding Background}"
- 249
-
-
-
-
-
- BorderBrush="{TemplateBinding BorderBrush}"
- 250
-
-
-
-
-
- BorderThickness="{TemplateBinding BorderThickness}"
- 251
-
-
-
-
-
- CornerRadius="{StaticResource TopLevelCornerRadius}">
- 252
-
-
-
-
-
- <Grid Margin="{StaticResource TopLevelContentMargin}">
- 253
-
-
-
-
-
-
-
- <Grid.ColumnDefinitions>
- 254
-
-
-
-
-
-
-
-
-
- <ColumnDefinition Width="Auto" />
- 255
-
-
-
-
-
-
-
-
-
- <ColumnDefinition Width="*" />
- 256
-
-
-
-
-
-
-
- </Grid.ColumnDefinitions>
- 257
- 258
-
-
-
-
-
-
-
-
- 259
-
-
-
-
-
-
-
- <ContentPresenter
- 260
-
-
-
-
-
-
-
-
-
- x:Name="Icon"
- 261
-
-
-
-
-
-
-
-
-
- Grid.Column="0"
- 262
-
-
-
-
-
-
-
-
-
- Margin="0,0,6,0"
- 263
-
-
-
-
-
-
-
-
-
- VerticalAlignment="Center"
- 264
-
-
-
-
-
-
-
-
-
- Content="{TemplateBinding Icon}"
- 265
-
-
-
-
-
-
-
-
-
- SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
- 266
- 267
-
-
-
-
-
-
-
-
- 268
-
-
-
-
-
-
-
- <ContentPresenter
- 269
-
-
-
-
-
-
-
-
-
- x:Name="HeaderPresenter"
- 270
-
-
-
-
-
-
-
-
-
- Grid.Column="1"
- 271
-
-
-
-
-
-
-
-
-
- Margin="{TemplateBinding Padding}"
- 272
-
-
-
-
-
-
-
-
-
- VerticalAlignment="Center"
- 273
-
-
-
-
-
-
-
-
-
- ContentSource="Header"
- 274
-
-
-
-
-
-
-
-
-
- RecognizesAccessKey="True"
- 275
-
-
-
-
-
-
-
-
-
- SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
- 276
-
-
-
-
-
-
-
-
-
- TextElement.Foreground="{TemplateBinding Foreground}" />
- 277
-
-
-
-
-
- </Grid>
- 278
-
-
-
- </Border>
- 279
-
-
-
- <ControlTemplate.Triggers>
- 280
-
-
-
-
-
-
- 281
-
-
-
-
-
- <Trigger Property="IsHighlighted" Value="True">
- 282
-
-
-
-
-
-
-
- <Setter TargetName="Border" Property="Background" Value="{DynamicResource MaskColor}" />
- 283
-
-
-
-
-
- </Trigger>
- 284
-
-
-
-
-
- <Trigger Property="Icon" Value="{x:Null}">
- 285
-
-
-
-
-
-
-
- <Setter TargetName="Icon" Property="Visibility" Value="Collapsed" />
- 286
-
-
-
-
-
- </Trigger>
- 287
-
-
-
-
-
- <Trigger Property="Header" Value="{x:Null}">
- 288
-
-
-
-
-
-
-
- <Setter TargetName="Icon" Property="Margin" Value="0" />
- 289
-
-
-
-
-
-
-
- <Setter TargetName="HeaderPresenter" Property="Visibility" Value="Collapsed" />
- 290
-
-
-
-
-
- </Trigger>
- 291
-
-
-
- </ControlTemplate.Triggers>
- 292
-
- </ControlTemplate>
- 293
- 294
-
-
- 295
-
- <ControlTemplate x:Key="{x:Static MenuItem.SubmenuItemTemplateKey}" TargetType="{x:Type MenuItem}">
- 296
-
-
-
- <Border
- 297
-
-
-
-
-
- x:Name="Border"
- 298
-
-
-
-
-
- Margin="{StaticResource MenuItemMargin}"
- 299
-
-
-
-
-
- Background="{TemplateBinding Background}"
- 300
-
-
-
-
-
- BorderBrush="{TemplateBinding BorderBrush}"
- 301
-
-
-
-
-
- BorderThickness="{TemplateBinding BorderThickness}"
- 302
-
-
-
-
-
- CornerRadius="{StaticResource MenuCornerRadius}">
- 303
-
-
-
-
-
- <Grid Margin="{StaticResource MenuItemContentPadding}">
- 304
-
-
-
-
-
-
-
- <Grid.ColumnDefinitions>
- 305
-
-
-
-
-
-
-
-
-
-
- 306
-
-
-
-
-
-
-
-
-
- <ColumnDefinition Width="Auto" SharedSizeGroup="MenuItemIconCol" />
- 307
-
-
-
-
-
-
-
-
-
-
- 308
-
-
-
-
-
-
-
-
-
- <ColumnDefinition Width="*" />
- 309
-
-
-
-
-
-
-
-
-
-
- 310
-
-
-
-
-
-
-
-
-
- <ColumnDefinition Width="Auto" SharedSizeGroup="MenuItemRightPartCol" />
- 311
-
-
-
-
-
-
-
- </Grid.ColumnDefinitions>
- 312
- 313
-
-
-
-
-
-
-
-
- 314
-
-
-
-
-
-
-
- <Border
- 315
-
-
-
-
-
-
-
-
-
- x:Name="CheckBoxIconBorder"
- 316
-
-
-
-
-
-
-
-
-
- Grid.Column="0"
- 317
-
-
-
-
-
-
-
-
-
- VerticalAlignment="Center"
- 318
-
-
-
-
-
-
-
-
-
- Visibility="Collapsed">
- 319
-
-
-
-
-
-
-
-
-
- <Path
- 320
-
-
-
-
-
-
-
-
-
-
-
- x:Name="CheckBoxIcon"
- 321
-
-
-
-
-
-
-
-
-
-
-
- Width="10"
- 322
-
-
-
-
-
-
-
-
-
-
-
- Height="10"
- 323
-
-
-
-
-
-
-
-
-
-
-
- HorizontalAlignment="Left"
- 324
-
-
-
-
-
-
-
-
-
-
-
- VerticalAlignment="Center"
- 325
-
-
-
-
-
-
-
-
-
-
-
- Fill="{TemplateBinding Foreground}"
- 326
-
-
-
-
-
-
-
-
-
-
-
- Stretch="Uniform" />
- 327
-
-
-
-
-
-
-
- </Border>
- 328
- 329
-
-
-
-
-
-
-
-
- 330
-
-
-
-
-
-
-
- <ContentPresenter
- 331
-
-
-
-
-
-
-
-
-
- x:Name="Icon"
- 332
-
-
-
-
-
-
-
-
-
- Grid.Column="0"
- 333
-
-
-
-
-
-
-
-
-
- Margin="0,0,6,0"
- 334
-
-
-
-
-
-
-
-
-
- VerticalAlignment="Center"
- 335
-
-
-
-
-
-
-
-
-
- Content="{TemplateBinding Icon}"
- 336
-
-
-
-
-
-
-
-
-
- SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
- 337
- 338
-
-
-
-
-
-
-
-
- 339
-
-
-
-
-
-
-
- <ContentPresenter
- 340
-
-
-
-
-
-
-
-
-
- Grid.Column="1"
- 341
-
-
-
-
-
-
-
-
-
- Margin="{TemplateBinding Padding}"
- 342
-
-
-
-
-
-
-
-
-
- VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
- 343
-
-
-
-
-
-
-
-
-
- ContentSource="Header"
- 344
-
-
-
-
-
-
-
-
-
- RecognizesAccessKey="True"
- 345
-
-
-
-
-
-
-
-
-
- SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
- 346
-
-
-
-
-
-
-
-
-
- TextElement.Foreground="{TemplateBinding Foreground}" />
- 347
- 348
-
-
-
-
-
-
-
-
- 349
-
-
-
-
-
-
-
- <TextBlock
- 350
-
-
-
-
-
-
-
-
-
- x:Name="InputGestureText"
- 351
-
-
-
-
-
-
-
-
-
- Grid.Column="2"
- 352
-
-
-
-
-
-
-
-
-
- Margin="25,0,0,0"
- 353
-
-
-
-
-
-
-
-
-
- VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
- 354
-
-
-
-
-
-
-
-
-
- DockPanel.Dock="Right"
- 355
-
-
-
-
-
-
-
-
-
- FontSize="11"
- 356
-
-
-
-
-
-
-
-
-
- Opacity="0.67"
- 357
-
-
-
-
-
-
-
-
-
- Text="{TemplateBinding InputGestureText}" />
- 358
-
-
-
-
-
- </Grid>
- 359
-
-
-
- </Border>
- 360
-
-
-
- <ControlTemplate.Triggers>
- 361
-
-
-
-
-
-
- 362
-
-
-
-
-
- <Trigger Property="IsHighlighted" Value="True">
- 363
-
-
-
-
-
-
-
- <Setter TargetName="Border" Property="Background" Value="{DynamicResource MaskColor}" />
- 364
-
-
-
-
-
- </Trigger>
- 365
-
-
-
-
-
-
- 366
-
-
-
-
-
- <Trigger Property="Icon" Value="{x:Null}">
- 367
-
-
-
-
-
-
-
- <Setter TargetName="Icon" Property="Visibility" Value="Collapsed" />
- 368
-
-
-
-
-
- </Trigger>
- 369
-
-
-
-
-
-
- 370
-
-
-
-
-
- <Trigger Property="IsCheckable" Value="True">
- 371
-
-
-
-
-
-
-
- <Setter TargetName="CheckBoxIconBorder" Property="Visibility" Value="Visible" />
- 372
-
-
-
-
-
- </Trigger>
- 373
-
-
-
-
-
-
- 374
-
-
-
-
-
- <Trigger Property="IsChecked" Value="True">
- 375
-
-
-
-
-
-
-
- <Setter TargetName="CheckBoxIcon" Property="Data" Value="{StaticResource CheckGraph}" />
- 376
-
-
-
-
-
- </Trigger>
- 377
-
-
-
-
-
-
- 378
-
-
-
-
-
- <Trigger Property="InputGestureText" Value="">
- 379
-
-
-
-
-
-
-
- <Setter TargetName="InputGestureText" Property="Visibility" Value="Collapsed" />
- 380
-
-
-
-
-
- </Trigger>
- 381
-
-
-
- </ControlTemplate.Triggers>
- 382
-
- </ControlTemplate>
- 383
- 384
-
-
- 385
-
- <ControlTemplate x:Key="{x:Static MenuItem.SubmenuHeaderTemplateKey}" TargetType="{x:Type MenuItem}">
- 386
-
-
-
- <Grid>
- 387
-
-
-
-
-
- <Grid.RowDefinitions>
- 388
-
-
-
-
-
-
-
- <RowDefinition Height="*" />
- 389
-
-
-
-
-
-
-
- <RowDefinition Height="Auto" />
- 390
-
-
-
-
-
- </Grid.RowDefinitions>
- 391
- 392
-
-
-
-
-
-
- 393
-
-
-
-
-
- <Border
- 394
-
-
-
-
-
-
-
- x:Name="Border"
- 395
-
-
-
-
-
-
-
- Grid.Row="1"
- 396
-
-
-
-
-
-
-
- Height="{TemplateBinding Height}"
- 397
-
-
-
-
-
-
-
- Margin="{StaticResource MenuItemMargin}"
- 398
-
-
-
-
-
-
-
- Background="Transparent"
- 399
-
-
-
-
-
-
-
- CornerRadius="{StaticResource MenuCornerRadius}">
- 400
-
-
-
-
-
-
-
- <Grid x:Name="MenuItemContent" Margin="{StaticResource MenuItemContentPadding}">
- 401
-
-
-
-
-
-
-
-
-
- <Grid.ColumnDefinitions>
- 402
-
-
-
-
-
-
-
-
-
-
-
- <ColumnDefinition Width="Auto" SharedSizeGroup="MenuItemIconCol" />
- 403
-
-
-
-
-
-
-
-
-
-
-
- <ColumnDefinition Width="*" />
- 404
-
-
-
-
-
-
-
-
-
-
-
- <ColumnDefinition Width="Auto" SharedSizeGroup="MenuItemRightPartCol" />
- 405
-
-
-
-
-
-
-
-
-
- </Grid.ColumnDefinitions>
- 406
- 407
-
-
-
-
-
-
-
-
-
-
- 408
-
-
-
-
-
-
-
-
-
- <ContentPresenter
- 409
-
-
-
-
-
-
-
-
-
-
-
- x:Name="Icon"
- 410
-
-
-
-
-
-
-
-
-
-
-
- Grid.Column="0"
- 411
-
-
-
-
-
-
-
-
-
-
-
- Margin="0,0,6,0"
- 412
-
-
-
-
-
-
-
-
-
-
-
- VerticalAlignment="Center"
- 413
-
-
-
-
-
-
-
-
-
-
-
- Content="{TemplateBinding Icon}"
- 414
-
-
-
-
-
-
-
-
-
-
-
- SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
- 415
- 416
-
-
-
-
-
-
-
-
-
-
- 417
-
-
-
-
-
-
-
-
-
- <ContentPresenter
- 418
-
-
-
-
-
-
-
-
-
-
-
- x:Name="HeaderHost"
- 419
-
-
-
-
-
-
-
-
-
-
-
- Grid.Column="1"
- 420
-
-
-
-
-
-
-
-
-
-
-
- Margin="{TemplateBinding Padding}"
- 421
-
-
-
-
-
-
-
-
-
-
-
- VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
- 422
-
-
-
-
-
-
-
-
-
-
-
- ContentSource="Header"
- 423
-
-
-
-
-
-
-
-
-
-
-
- RecognizesAccessKey="True"
- 424
-
-
-
-
-
-
-
-
-
-
-
- SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
- 425
- 426
-
-
-
-
-
-
-
-
-
-
- 427
-
-
-
-
-
-
-
-
-
- <Path
- 428
-
-
-
-
-
-
-
-
-
-
-
- Grid.Column="2"
- 429
-
-
-
-
-
-
-
-
-
-
-
- Width="10"
- 430
-
-
-
-
-
-
-
-
-
-
-
- Height="10"
- 431
-
-
-
-
-
-
-
-
-
-
-
- Margin="0,0,4,0"
- 432
-
-
-
-
-
-
-
-
-
-
-
- HorizontalAlignment="Right"
- 433
-
-
-
-
-
-
-
-
-
-
-
- VerticalAlignment="Center"
- 434
-
-
-
-
-
-
-
-
-
-
-
- Data="{StaticResource ForwardGraph}"
- 435
-
-
-
-
-
-
-
-
-
-
-
- Fill="{TemplateBinding Foreground}"
- 436
-
-
-
-
-
-
-
-
-
-
-
- Opacity="0.67"
- 437
-
-
-
-
-
-
-
-
-
-
-
- Stretch="Uniform" />
- 438
-
-
-
-
-
-
-
- </Grid>
- 439
-
-
-
-
-
- </Border>
- 440
- 441
-
-
-
-
-
-
- 442
-
-
-
-
-
- <local:FluentPopup
- 443
-
-
-
-
-
-
-
- x:Name="PART_Popup"
- 444
-
-
-
-
-
-
-
- Grid.Row="1"
- 445
-
-
-
-
-
-
-
- Focusable="False"
- 446
-
-
-
-
-
-
-
- IsOpen="{TemplateBinding IsSubmenuOpen}"
- 447
-
-
-
-
-
-
-
- Placement="Right"
- 448
-
-
-
-
-
-
-
- PlacementTarget="{Binding ElementName=MenuItemContent}"
- 449
-
-
-
-
-
-
-
- PopupAnimation="None">
- 450
-
-
-
-
-
-
-
- <Grid x:Name="PopupRoot" Background="{DynamicResource PopupWindowBackground}">
- 451
-
-
-
-
-
-
-
-
-
-
- 452
-
-
-
-
-
-
-
-
-
- <Grid.RenderTransform>
- 453
-
-
-
-
-
-
-
-
-
-
-
- <TranslateTransform />
- 454
-
-
-
-
-
-
-
-
-
- </Grid.RenderTransform>
- 455
-
-
-
-
-
-
-
-
-
- <ScrollViewer
- 456
-
-
-
-
-
-
-
-
-
-
-
- Padding="{StaticResource MenuBorderPadding}"
- 457
-
-
-
-
-
-
-
-
-
-
-
- CanContentScroll="True"
- 458
-
-
-
-
-
-
-
-
-
-
-
- Style="{StaticResource MenuItemScrollViewerStyle}">
- 459
-
-
-
-
-
-
-
-
-
-
-
-
- 460
-
-
-
-
-
-
-
-
-
-
-
- <ItemsPresenter
- 461
-
-
-
-
-
-
-
-
-
-
-
-
-
- x:Name="ItemsPresenter"
- 462
-
-
-
-
-
-
-
-
-
-
-
-
-
- Grid.IsSharedSizeScope="True"
- 463
-
-
-
-
-
-
-
-
-
-
-
-
-
- KeyboardNavigation.DirectionalNavigation="Cycle"
- 464
-
-
-
-
-
-
-
-
-
-
-
-
-
- KeyboardNavigation.TabNavigation="Cycle"
- 465
-
-
-
-
-
-
-
-
-
-
-
-
-
- SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
- 466
-
-
-
-
-
-
-
-
-
- </ScrollViewer>
- 467
-
-
-
-
-
-
-
- </Grid>
- 468
-
-
-
-
-
- </local:FluentPopup>
- 469
-
-
-
- </Grid>
- 470
-
-
-
- <ControlTemplate.Triggers>
- 471
-
-
-
-
-
-
- 472
-
-
-
-
-
- <Trigger Property="Icon" Value="{x:Null}">
- 473
-
-
-
-
-
-
-
- <Setter TargetName="Icon" Property="Visibility" Value="Collapsed" />
- 474
-
-
-
-
-
-
-
- <Setter TargetName="Icon" Property="Margin" Value="0" />
- 475
-
-
-
-
-
- </Trigger>
- 476
-
-
-
-
-
-
- 477
-
-
-
-
-
- <Trigger Property="IsHighlighted" Value="true">
- 478
-
-
-
-
-
-
-
- <Setter TargetName="Border" Property="Background" Value="{DynamicResource MaskColor}" />
- 479
-
-
-
-
-
- </Trigger>
- 480
-
-
-
-
-
-
- 481
-
-
-
-
-
- <Trigger Property="IsSubmenuOpen" Value="True">
- 482
-
-
-
-
-
-
-
- <Trigger.EnterActions>
- 483
-
-
-
-
-
-
-
-
-
- <BeginStoryboard>
- 484
-
-
-
-
-
-
-
-
-
-
-
- <Storyboard>
- 485
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- 486
-
-
-
-
-
-
-
-
-
-
-
-
-
- <DoubleAnimation
- 487
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- Storyboard.TargetName="PopupRoot"
- 488
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- Storyboard.TargetProperty="(Grid.RenderTransform).(TranslateTransform.Y)"
- 489
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- From="-45"
- 490
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- To="0"
- 491
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- Duration="{StaticResource MenuAnimationDuration}">
- 492
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- <DoubleAnimation.EasingFunction>
- 493
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- <CircleEase EasingMode="EaseOut" />
- 494
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- </DoubleAnimation.EasingFunction>
- 495
-
-
-
-
-
-
-
-
-
-
-
-
-
- </DoubleAnimation>
- 496
-
-
-
-
-
-
-
-
-
-
-
- </Storyboard>
- 497
-
-
-
-
-
-
-
-
-
- </BeginStoryboard>
- 498
-
-
-
-
-
-
-
- </Trigger.EnterActions>
- 499
-
-
-
-
-
- </Trigger>
- 500
-
-
-
- </ControlTemplate.Triggers>
- 501
-
- </ControlTemplate>
- 502
- 503
-
-
- 504
-
-
- 539
- 540 </ResourceDictionary>
复制代码 View Code三、应用模板和样式到全局
将上面的资源字典归并到应用步调资源中,然后为ContextMenu和MenuItem指定默认样式:注意,ContextMenu必要利用之前文章中的FluentTooltip.UseFluentStyle来实现亚克力材质殊效。其内部原理都是反射获取popup的hwnd句柄,然后附加WindowMaterial殊效。
参考文档
1. ContextMenu Styles and Templates WPF | Microsoft Learn
2. Menu Styles and Templates WPF | Microsoft Learn
3. PresentationFramework.Fluent/Themes/Fluent.xaml | GitHub
本作品接纳 知识共享署名-非贸易性利用-雷同方式共享 4.0 国际允许协议 举行允许。欢迎转载、利用、重新发布,但务必生存文章署名TwilightLemon(https://blog.twlmgatito.cn),不得用于贸易目的,基于本文修改后的作品务必以雷同的允许发布。
文章及其代码堆栈大概不时更新,检察原文:WPF 为ContextMenu利用Fluent风格的亚克力材质殊效 - Twlm's Blog
|