首页 > 解决方案 > MenuItem 在 IsPressed 后不会触发 IsHighlighted

问题描述

我有一个 MenuItem 样式,效果很好,但是当我选择(单击)菜单项时,它不会再次触发“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>

按菜单后如何使其工作?如果我删除按下触发器它工作正常:\

标签: c#wpfwpf-style

解决方案


将触发器中的<BeginStoryboard>元素替换ExitAction为a并将动画的属性设置为:IsHighlightedRemoveStoryboardFillBehaviorIsPressedStop

<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>

推荐阅读