【问题标题】:How to remove ContextMenu highlight effect?如何去除 ContextMenu 高亮效果?
【发布时间】:2016-06-23 19:34:05
【问题描述】:

我在一个矩形上附加了以下上下文菜单:

<Rectangle x:Name="criteria1" IsHitTestVisible="True">
 <Rectangle.ContextMenu>
    <ContextMenu>
        <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height=".5*"/>
                    <RowDefinition />
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width=".5*"/>
                    <ColumnDefinition />
                </Grid.ColumnDefinitions>
                <Label Grid.Column="0" Grid.Row="0" Content="Amplitude percentage"/>
                <TextBox Grid.Column="1" Grid.Row="0" mah:TextBoxHelper.ClearTextButton="True" mah:ControlsHelper.FocusBorderBrush="{DynamicResource AccentColorBrush}" Width="70" >
                    <TextBox.Text>
                        <Binding Path="Criteria1AmplitudePercentage" TargetNullValue="{x:Static sys:String.Empty}" UpdateSourceTrigger="LostFocus" ValidatesOnDataErrors="True" />
                    </TextBox.Text>
                </TextBox>
                <Label Grid.Column="0" Grid.Row="1" Content="Bandwidth percentage}" />
                <TextBox Grid.Column="1" Grid.Row="1" mah:TextBoxHelper.ClearTextButton="True" mah:ControlsHelper.FocusBorderBrush="{DynamicResource AccentColorBrush}" Width="70" >
                    <TextBox.Text>
                        <Binding Path="Criteria1BandwidthPercentage" TargetNullValue="{x:Static sys:String.Empty}" UpdateSourceTrigger="LostFocus" ValidatesOnDataErrors="True" />
                    </TextBox.Text>
                </TextBox>
            </Grid>
    </ContextMenu>
</Rectangle.ContextMenu>

在鼠标悬停时,我会在背景变为深灰色的地方获得这种突出显示效果:

如何消除这种突出显示效果?

更新

上下文菜单只能通过右键单击出现。设置ContextMenu Background="Red" 和Grid Background="Red" 时,菜单如下所示:

当鼠标进入或位于菜单内时,或者即使我在文本框内单击,它看起来像这样:

编辑:这是默认的ContextMenu 样式:

        <Style x:Key="ContextMenuDefaultStyle" TargetType="{x:Type ContextMenu}">
        <Setter Property="OverridesDefaultStyle" Value="True"/>
        <Setter Property="Background" Value="{DynamicResource MenuPopupBackgroundBrush}"/>
        <Setter Property="BorderThickness" Value="1"/>
        <Setter Property="BorderBrush" Value="{DynamicResource MenuPopupBorderBrush}"/>
        <Setter Property="VerticalContentAlignment" Value="Center"/>
        <Setter Property="Padding" Value="2"/>
        <Setter Property="Grid.IsSharedSizeScope" Value="True"/>
        <Setter Property="HasDropShadow" Value="{DynamicResource {x:Static SystemParameters.DropShadowKey}}"/>
        <Setter Property="ScrollViewer.PanningMode" Value="Both"/>
        <Setter Property="Stylus.IsFlicksEnabled" Value="False"/>
        <Setter Property="FontFamily" Value="{DynamicResource {x:Static SystemFonts.MessageFontFamilyKey}}"/>
        <Setter Property="FontSize" Value="{DynamicResource {x:Static SystemFonts.MessageFontSizeKey}}"/>
        <Setter Property="FontWeight" Value="{DynamicResource {x:Static SystemFonts.MessageFontWeightKey}}"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ContextMenu}">
                    <Border Uid="Border_93">
                        <Border.Style>
                            <Style TargetType="{x:Type Border}">
                                <Setter Property="Tag" Value="{DynamicResource {x:Static SystemParameters.DropShadowKey}}"/>
                                <Style.Triggers>
                                    <DataTrigger Binding="{Binding Tag, RelativeSource={RelativeSource Self}}" Value="True">
                                        <Setter Property="Background" Value="Transparent"/>
                                        <Setter Property="Padding" Value="0,0,5,5"/>
                                        <Setter Property="Effect">
                                            <Setter.Value>
                                                <DropShadowEffect BlurRadius="4" Opacity="0.8" ShadowDepth="1"/>
                                            </Setter.Value>
                                        </Setter>
                                    </DataTrigger>
                                </Style.Triggers>
                            </Style>
                        </Border.Style>
                        <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Uid="Border_50">
                            <ScrollViewer CanContentScroll="True" Style="{DynamicResource {ComponentResourceKey ResourceId=MenuScrollViewer, TypeInTargetAssembly={x:Type FrameworkElement}}}" Uid="ScrollViewer_9">
                                <ItemsPresenter KeyboardNavigation.DirectionalNavigation="Cycle" Margin="{TemplateBinding Padding}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" Uid="ItemsPresenter_5"/>
                            </ScrollViewer>
                        </Border>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

【问题讨论】:

  • 您只需要在控件的样式模板中将其杀死,我无法在他们的文档中找到它,但如果您将其提取并发布在这里,我相信有人可以快速向您展示要丢弃的部分。或者只是看到了弹出窗口的 ContextMenu
  • 不,这是一个例子。我在您的问题中添加了默认的 ContextMenu 样式。请看我的编辑。
  • @jstreet 因为他使用的是 MahApps Metro,所以它带有自己的 ContextMenu 控件,我认为这会导致提供有问题的 MouseOver 效果的覆盖行为。这就是本例中需要的样式模板。 CristianM 请右键单击您的,选择编辑模板 -> 编辑副本,并提供该副本。
  • @ChrisW:我看到的唯一ContextMenu 来自System.Windows.Controls 命名空间。请指出Mahapps 命名空间内的位置是它自己的ContextMenu。

标签: c# wpf xaml mahapps.metro


【解决方案1】:
<ContextMenu Background="Transparent">

这还不够吗?

【讨论】:

  • 不是透明,而是设置为 - 红色。整个 ContextMenu 会变红吗?
  • @CristianM 等等,我刚刚再次阅读了您的问题。 “鼠标悬停”是什么意思?上下文菜单是否仅通过右键单击出现?如果问题只在“悬停”期间真正出现,那么您应该向我们展示它在“悬停”之前的样子,以及您悬停的具体内容。
猜你喜欢
  • 2013-05-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-03
相关资源
最近更新 更多