如何在鼠标悬停期间强调MenuItem.Header属性?

时间:2014-09-15 12:55:50

标签: wpf xaml .net-4.0

我有一个带有几个顶级MenuItems +孩子的标准菜单。 controltemplate看起来像这样:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <Style x:Key="{x:Type Menu}" TargetType="{x:Type Menu}">
        <Setter Property="Foreground" Value="{DynamicResource LinkTextColorBrush}" />
        <Setter Property="Cursor" Value="Hand" />
        <Setter Property="OverridesDefaultStyle" Value="True" />
        <Setter Property="SnapsToDevicePixels" Value="True" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Menu}">
                    <Border BorderThickness="0">
                        <StackPanel ClipToBounds="True" Orientation="Horizontal" IsItemsHost="True" />
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <ControlTemplate x:Key="{x:Static MenuItem.TopLevelHeaderTemplateKey}" TargetType="{x:Type MenuItem}">
        <Border x:Name="Border">
            <Grid>
                <ContentPresenter Margin="6,3,6,3"  ContentSource="Header" RecognizesAccessKey="True" />
                <Popup x:Name="Popup" Placement="Bottom" IsOpen="{TemplateBinding IsSubmenuOpen}" AllowsTransparency="True" Focusable="False" PopupAnimation="Fade">
                    <Border x:Name="SubmenuBorder" SnapsToDevicePixels="True" BorderThickness="1" Background="GhostWhite">
                        <Border.BorderBrush>
                            <SolidColorBrush  Color="{Binding Source={StaticResource CstBorderLightGrey}, Path=Color}" />
                        </Border.BorderBrush>
                        <StackPanel IsItemsHost="True" KeyboardNavigation.DirectionalNavigation="Cycle" />
                    </Border>
                </Popup>
            </Grid>
        </Border>
        <ControlTemplate.Triggers>
            <Trigger Property="IsSuspendingPopupAnimation" Value="true">
                <Setter TargetName="Popup" Property="PopupAnimation" Value="None" />
            </Trigger>
            <Trigger Property="IsHighlighted" Value="true">
                <Setter Property="Foreground" Value="{DynamicResource HotLinkTextColorBrush}" />
                <Setter Property="BorderBrush" TargetName="Border" Value="Transparent" />
                <Setter Property="Background" TargetName="Border" Value="White" />
            </Trigger>
            <Trigger SourceName="Popup" Property="AllowsTransparency" Value="True">
                <Setter TargetName="SubmenuBorder" Property="CornerRadius" Value="0,0,4,4" />
                <Setter TargetName="SubmenuBorder" Property="Padding" Value="0,0,0,3" />
            </Trigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>

    <ControlTemplate x:Key="{x:Static MenuItem.TopLevelItemTemplateKey}" TargetType="{x:Type MenuItem}">
        <Border x:Name="Border">
            <Grid>
                <ContentPresenter Margin="6,3,6,3" ContentSource="Header" RecognizesAccessKey="True" />
                <Popup x:Name="Popup" Placement="Bottom" IsOpen="{TemplateBinding IsSubmenuOpen}" AllowsTransparency="True" Focusable="False" PopupAnimation="Fade">
                    <Border x:Name="SubmenuBorder" SnapsToDevicePixels="True" BorderThickness="1" Background="GhostWhite">
                        <Border.BorderBrush>

                            <SolidColorBrush  Color="{Binding Source={StaticResource CstBorderLightGrey}, Path=Color}" />
                        </Border.BorderBrush>
                        <StackPanel IsItemsHost="True" KeyboardNavigation.DirectionalNavigation="Cycle" />
                    </Border>
                </Popup>
            </Grid>
        </Border>
        <ControlTemplate.Triggers>
            <Trigger Property="IsSuspendingPopupAnimation" Value="true">
                <Setter TargetName="Popup" Property="PopupAnimation" Value="None" />
            </Trigger>
            <Trigger Property="IsHighlighted" Value="true">
                <Setter Property="Foreground" Value="{DynamicResource HotLinkTextColorBrush}" />
                <Setter Property="BorderBrush" TargetName="Border" Value="Transparent" />
                <Setter Property="Background" TargetName="Border" Value="White" />
            </Trigger>
            <Trigger SourceName="Popup" Property="AllowsTransparency" Value="True">
                <Setter TargetName="SubmenuBorder" Property="CornerRadius" Value="0,0,4,4" />
                <Setter TargetName="SubmenuBorder" Property="Padding" Value="0,0,0,3" />
            </Trigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>
</ResourceDictionary>

还有一些小的调整,但除此之外它工作正常。但是我遇到了一个问题,我需要在鼠标悬停期间强调toplevelitems。我很长一段时间一直在寻找有关这方面的信息,到目前为止我还没有提出任何建议。

1 个答案:

答案 0 :(得分:0)

您可以使用MenuItem.Header属性将带有下划线的TextBlock添加到菜单项标题部分:

<MenuItem>
    <MenuItem.Header>
        <TextBlock Text="{Binding YourHeaderProperty}" TextDecorations="Underline" />
    </MenuItem.Header>
</MenuItem>

值得指出的是,可能会在您执行此操作时混淆您的用户,因为带下划线的文本通常表示它是超链接。


更新&gt;&gt;&gt;

您只需稍微重新排列代码并添加DataTrigger即可进行更改:

<MenuItem>
    <MenuItem.Header>
        <TextBlock Text="rtuwruhey5uje5yu">
            <TextBlock.Style>
                <Style TargetType="{x:Type TextBlock}">
                    <Setter Property="TextDecorations" Value="None" />
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding IsMouseOver, RelativeSource={
                            RelativeSource AncestorType={x:Type MenuItem}}}" 
                            Value="True">
                            <Setter Property="TextDecorations" Value="Underline" />
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </TextBlock.Style>
        </TextBlock>
    </MenuItem.Header>
</MenuItem>