【问题标题】:Styling MouseOver SubMenuItem WPF/XAML样式化 MouseOver SubMenuItem WPF/XAML
【发布时间】:2017-07-10 15:22:14
【问题描述】:

我目前正在尝试以与顶级 MenuItem 类似的方式设置 Submenu/SubmenuHeader 的样式,但是我想将背景和边框更改为不同的颜色(不是默认颜色)

我可以在模板的哪个位置进行设置?还是有更好的方法来设置子菜单的样式?

这是我的 MenuItem 模板代码:

  <!-- MenuItem Template -->
    <ControlTemplate
        x:Key="MenuItem"
        TargetType="MenuItem">
        <Border
            Name="border"
            BorderThickness="1"
            BorderBrush="Transparent"
            Background="{TemplateBinding Background}">
            <Grid>
                <ContentPresenter
                    HorizontalAlignment="Center"
                    VerticalAlignment="Center"
                    ContentSource="Header" />
                <Popup
                    Name="Popup"
                    Placement="Bottom"
                    IsOpen="{TemplateBinding IsSubmenuOpen}"
                    AllowsTransparency="True"
                    Focusable="False"
                    PopupAnimation="Fade">
                    <Border
                        Name="SubmenuBorder"
                        SnapsToDevicePixels="True"
                        Background="{StaticResource ResourceKey=DarkGrayBrush}"
                        BorderBrush="Transparent"
                        BorderThickness="1">
                        <StackPanel
                            Name="SubMenu"
                            IsItemsHost="True"
                            KeyboardNavigation.DirectionalNavigation="Cycle" />
                    </Border>
                </Popup>
            </Grid>
        </Border>
        <ControlTemplate.Triggers>
            <Trigger
                Property="IsMouseOver"
                Value="True">
                <Setter
                    TargetName="border"
                    Property="BorderBrush"
                    Value="Transparent" />
                <Setter
                    Property="Background"
                    Value="#00948d" />
            </Trigger>
            <Trigger
                Property="IsSuspendingPopupAnimation"
                Value="true">
                <Setter
                    TargetName="Popup"
                    Property="PopupAnimation"
                    Value="None" />
            </Trigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>

【问题讨论】:

    标签: wpf xaml


    【解决方案1】:

    使用样式和VisualState PointerOver:

                <Style x:Key="MenuItemStyle" TargetType="MenuItem">
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="MenuItem">
                                <Grid x:Name="Root">
                                    <VisualStateManager.VisualStateGroups>
                                        <VisualStateGroup x:Name="MenuItemCommonStates">
                                            <VisualState x:Name="PointerOver">
                                                <Storyboard>
                                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="Root"
                                                                                   Storyboard.TargetProperty="Background">
                                                        <DiscreteObjectKeyFrame KeyTime="0"
                                                                                Value="{ThemeResource AppBarBackgroundThemeBrush}" />
                                                    </ObjectAnimationUsingKeyFrames>
                                                </Storyboard>
                                            </VisualState>
                                        </VisualStateGroup>
                                    </VisualStateManager.VisualStateGroups>
                                </Grid>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
    

    【讨论】:

    • 将这个添加到我的模板/样式之上!谢谢!
    猜你喜欢
    • 2015-10-23
    • 2011-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多