MenuItem 在 IsPressed 后不会触发 IsHighlighted
MenuItem dosen't fire IsHighlighted after IsPressed
我有一个 MenuItem 样式,效果很好,但是当我 select(单击)菜单项时,它不会再次触发 "IsMouseOver" 或 "IsHighlighted"。
我尝试使用 IsKeyboardFocused 来修复它,但它会杀死另一侧的 IsPressed,所以我也无法使用它。
这是我的风格代码:
<Style TargetType="{x:Type MenuItem}" x:Key="MyCustomMenuItem">
<Setter Property="OverridesDefaultStyle" Value="true" />
<Setter Property="FontFamily" Value="{DynamicResource MenuFontFamily}"/>
<Setter Property="Focusable" Value="False"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type MenuItem}">
<Border x:Name="brd" Background="{StaticResource MainBrush}" >
<Grid>
<ContentPresenter Content="{TemplateBinding Header}" Margin="10,3,10,3" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" />
<Popup x:Name="PART_Popup" Placement="Right" AllowsTransparency="True" Focusable="False" IsOpen="{Binding IsSubmenuOpen, RelativeSource={RelativeSource TemplatedParent}}" PopupAnimation="{DynamicResource {x:Static SystemParameters.MenuPopupAnimationKey}}">
<Border x:Name="SubMenuBorder" BorderBrush="{DynamicResource KeyColor}" BorderThickness="1" Background="{DynamicResource MainBrushTransparent}" Padding="2">
<ScrollViewer x:Name="SubMenuScrollViewer" Style="{DynamicResource {ComponentResourceKey ResourceId=MenuScrollViewer, TypeInTargetAssembly={x:Type FrameworkElement}}}">
<Grid RenderOptions.ClearTypeHint="Enabled">
<Canvas HorizontalAlignment="Left" Height="0" VerticalAlignment="Top" Width="0">
<Rectangle x:Name="OpaqueRect" Fill="{DynamicResource MainBrushTransparent}" Height="{Binding ActualHeight, ElementName=SubMenuBorder}" Width="{Binding ActualWidth, ElementName=SubMenuBorder}"/>
</Canvas>
<ItemsPresenter x:Name="ItemsPresenter" KeyboardNavigation.DirectionalNavigation="Cycle" Grid.IsSharedSizeScope="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" KeyboardNavigation.TabNavigation="Cycle"/>
</Grid>
</ScrollViewer>
</Border>
</Popup>
</Grid>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsHighlighted" Value="True">
<Trigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<ColorAnimation Storyboard.TargetName="brd" Duration="0:0:0.300" To="#349E7E" Storyboard.TargetProperty="(Border.Background).(SolidColorBrush.Color)" />
<ColorAnimation Duration="0:0:0.300" To="#FFEAEAEA" Storyboard.TargetProperty="Foreground.Color" />
</Storyboard>
</BeginStoryboard>
</Trigger.EnterActions>
<Trigger.ExitActions>
<BeginStoryboard>
<Storyboard>
<ColorAnimation Storyboard.TargetName="brd" Duration="0:0:0.300" To="#2C2C2C" Storyboard.TargetProperty="(Border.Background).(SolidColorBrush.Color)" />
<ColorAnimation Duration="0:0:0.300" To="#FF8B8B8B" Storyboard.TargetProperty="Foreground.Color" />
</Storyboard>
</BeginStoryboard>
</Trigger.ExitActions>
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Trigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<ColorAnimation Storyboard.TargetName="brd" Duration="0:0:0.300" To="#FF34008D" Storyboard.TargetProperty="(Border.Background).(SolidColorBrush.Color)" />
</Storyboard>
</BeginStoryboard>
</Trigger.EnterActions>
<Trigger.ExitActions>
<BeginStoryboard>
<Storyboard>
<ColorAnimation Storyboard.TargetName="brd" Duration="0:0:0.300" To="#2C2C2C" Storyboard.TargetProperty="(Border.Background).(SolidColorBrush.Color)" />
</Storyboard>
</BeginStoryboard>
</Trigger.ExitActions>
</Trigger>
<Trigger Property="IsSubmenuOpen" Value="True">
<Trigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<ColorAnimation Storyboard.TargetName="brd" Duration="0:0:0.300" To="#349E7E" Storyboard.TargetProperty="(Border.Background).(SolidColorBrush.Color)" />
<ColorAnimation Duration="0:0:0.300" To="#FFEAEAEA" Storyboard.TargetProperty="Foreground.Color" />
</Storyboard>
</BeginStoryboard>
</Trigger.EnterActions>
<Trigger.ExitActions>
<BeginStoryboard>
<Storyboard>
<ColorAnimation Storyboard.TargetName="brd" Duration="0:0:0.300" To="#2C2C2C" Storyboard.TargetProperty="(Border.Background).(SolidColorBrush.Color)" />
<ColorAnimation Duration="0:0:0.300" To="#FF8B8B8B" Storyboard.TargetProperty="Foreground.Color" />
</Storyboard>
</BeginStoryboard>
</Trigger.ExitActions>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
按下菜单后如何让它工作?
如果我删除 press trigger 它工作正常:\
将 IsHighlighted
触发器的 ExitAction
中的 <BeginStoryboard>
元素替换为 RemoveStoryboard
并设置 FillBehavior
属性 IsPressed
动画到Stop
:
<Trigger Property="IsHighlighted" Value="True">
<Trigger.EnterActions>
<BeginStoryboard x:Name="sb1">
<Storyboard>
<ColorAnimation Storyboard.TargetName="brd" Duration="0:0:0.300" To="#349E7E" Storyboard.TargetProperty="(Border.Background).(SolidColorBrush.Color)" />
<ColorAnimation Duration="0:0:0.300" To="#FFEAEAEA" Storyboard.TargetProperty="Foreground.Color" />
</Storyboard>
</BeginStoryboard>
</Trigger.EnterActions>
<Trigger.ExitActions>
<RemoveStoryboard BeginStoryboardName="sb1" />
</Trigger.ExitActions>
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Trigger.EnterActions>
<BeginStoryboard x:Name="sb2">
<Storyboard>
<ColorAnimation Storyboard.TargetName="brd" Duration="0:0:0.300" To="#FF34008D"
FillBehavior="Stop"
Storyboard.TargetProperty="(Border.Background).(SolidColorBrush.Color)" />
</Storyboard>
</BeginStoryboard>
</Trigger.EnterActions>
</Trigger>
我有一个 MenuItem 样式,效果很好,但是当我 select(单击)菜单项时,它不会再次触发 "IsMouseOver" 或 "IsHighlighted"。
我尝试使用 IsKeyboardFocused 来修复它,但它会杀死另一侧的 IsPressed,所以我也无法使用它。
这是我的风格代码:
<Style TargetType="{x:Type MenuItem}" x:Key="MyCustomMenuItem">
<Setter Property="OverridesDefaultStyle" Value="true" />
<Setter Property="FontFamily" Value="{DynamicResource MenuFontFamily}"/>
<Setter Property="Focusable" Value="False"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type MenuItem}">
<Border x:Name="brd" Background="{StaticResource MainBrush}" >
<Grid>
<ContentPresenter Content="{TemplateBinding Header}" Margin="10,3,10,3" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" />
<Popup x:Name="PART_Popup" Placement="Right" AllowsTransparency="True" Focusable="False" IsOpen="{Binding IsSubmenuOpen, RelativeSource={RelativeSource TemplatedParent}}" PopupAnimation="{DynamicResource {x:Static SystemParameters.MenuPopupAnimationKey}}">
<Border x:Name="SubMenuBorder" BorderBrush="{DynamicResource KeyColor}" BorderThickness="1" Background="{DynamicResource MainBrushTransparent}" Padding="2">
<ScrollViewer x:Name="SubMenuScrollViewer" Style="{DynamicResource {ComponentResourceKey ResourceId=MenuScrollViewer, TypeInTargetAssembly={x:Type FrameworkElement}}}">
<Grid RenderOptions.ClearTypeHint="Enabled">
<Canvas HorizontalAlignment="Left" Height="0" VerticalAlignment="Top" Width="0">
<Rectangle x:Name="OpaqueRect" Fill="{DynamicResource MainBrushTransparent}" Height="{Binding ActualHeight, ElementName=SubMenuBorder}" Width="{Binding ActualWidth, ElementName=SubMenuBorder}"/>
</Canvas>
<ItemsPresenter x:Name="ItemsPresenter" KeyboardNavigation.DirectionalNavigation="Cycle" Grid.IsSharedSizeScope="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" KeyboardNavigation.TabNavigation="Cycle"/>
</Grid>
</ScrollViewer>
</Border>
</Popup>
</Grid>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsHighlighted" Value="True">
<Trigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<ColorAnimation Storyboard.TargetName="brd" Duration="0:0:0.300" To="#349E7E" Storyboard.TargetProperty="(Border.Background).(SolidColorBrush.Color)" />
<ColorAnimation Duration="0:0:0.300" To="#FFEAEAEA" Storyboard.TargetProperty="Foreground.Color" />
</Storyboard>
</BeginStoryboard>
</Trigger.EnterActions>
<Trigger.ExitActions>
<BeginStoryboard>
<Storyboard>
<ColorAnimation Storyboard.TargetName="brd" Duration="0:0:0.300" To="#2C2C2C" Storyboard.TargetProperty="(Border.Background).(SolidColorBrush.Color)" />
<ColorAnimation Duration="0:0:0.300" To="#FF8B8B8B" Storyboard.TargetProperty="Foreground.Color" />
</Storyboard>
</BeginStoryboard>
</Trigger.ExitActions>
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Trigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<ColorAnimation Storyboard.TargetName="brd" Duration="0:0:0.300" To="#FF34008D" Storyboard.TargetProperty="(Border.Background).(SolidColorBrush.Color)" />
</Storyboard>
</BeginStoryboard>
</Trigger.EnterActions>
<Trigger.ExitActions>
<BeginStoryboard>
<Storyboard>
<ColorAnimation Storyboard.TargetName="brd" Duration="0:0:0.300" To="#2C2C2C" Storyboard.TargetProperty="(Border.Background).(SolidColorBrush.Color)" />
</Storyboard>
</BeginStoryboard>
</Trigger.ExitActions>
</Trigger>
<Trigger Property="IsSubmenuOpen" Value="True">
<Trigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<ColorAnimation Storyboard.TargetName="brd" Duration="0:0:0.300" To="#349E7E" Storyboard.TargetProperty="(Border.Background).(SolidColorBrush.Color)" />
<ColorAnimation Duration="0:0:0.300" To="#FFEAEAEA" Storyboard.TargetProperty="Foreground.Color" />
</Storyboard>
</BeginStoryboard>
</Trigger.EnterActions>
<Trigger.ExitActions>
<BeginStoryboard>
<Storyboard>
<ColorAnimation Storyboard.TargetName="brd" Duration="0:0:0.300" To="#2C2C2C" Storyboard.TargetProperty="(Border.Background).(SolidColorBrush.Color)" />
<ColorAnimation Duration="0:0:0.300" To="#FF8B8B8B" Storyboard.TargetProperty="Foreground.Color" />
</Storyboard>
</BeginStoryboard>
</Trigger.ExitActions>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
按下菜单后如何让它工作? 如果我删除 press trigger 它工作正常:\
将 IsHighlighted
触发器的 ExitAction
中的 <BeginStoryboard>
元素替换为 RemoveStoryboard
并设置 FillBehavior
属性 IsPressed
动画到Stop
:
<Trigger Property="IsHighlighted" Value="True">
<Trigger.EnterActions>
<BeginStoryboard x:Name="sb1">
<Storyboard>
<ColorAnimation Storyboard.TargetName="brd" Duration="0:0:0.300" To="#349E7E" Storyboard.TargetProperty="(Border.Background).(SolidColorBrush.Color)" />
<ColorAnimation Duration="0:0:0.300" To="#FFEAEAEA" Storyboard.TargetProperty="Foreground.Color" />
</Storyboard>
</BeginStoryboard>
</Trigger.EnterActions>
<Trigger.ExitActions>
<RemoveStoryboard BeginStoryboardName="sb1" />
</Trigger.ExitActions>
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Trigger.EnterActions>
<BeginStoryboard x:Name="sb2">
<Storyboard>
<ColorAnimation Storyboard.TargetName="brd" Duration="0:0:0.300" To="#FF34008D"
FillBehavior="Stop"
Storyboard.TargetProperty="(Border.Background).(SolidColorBrush.Color)" />
</Storyboard>
</BeginStoryboard>
</Trigger.EnterActions>
</Trigger>