WPF-ContextMenu - 如何禁用鼠标悬停或焦点时的背景更改

问题描述 投票:0回答:2

这是我在 StackOverFlow 中的第一个问题,

我有一个文本框,单击时,它会显示一个上下文菜单,我在上下文菜单中有一些控件(用户控件或..)。

一切都很好,除了鼠标悬停时,我所有的控件都获得焦点并且它们的背景变为蓝色,这太糟糕了, 另一个问题,在上下文菜单中,有一条垂直线,并且其左侧有一个图标,我该如何删除它?

C# 代码:

private void textBox1_GotMouseCapture(object sender, MouseEventArgs e)
    {

        textBox1.ContextMenu.PlacementTarget = textBox1;
        textBox1.ContextMenu.IsOpen = true;
        textBox1.Focus();
}

和 XAML 代码:

<TextBox Height="23" HorizontalAlignment="Left" Margin="12,55,0,0"
Name="textBox1" VerticalAlignment="Top"
Width="120" MouseDown="textBox1_MouseDown" 
GotMouseCapture="textBox1_GotMouseCapture"
ContextMenuService.HasDropShadow="False" 
ContextMenuService.ShowOnDisabled="True" 
TextChanged="textBox1_TextChanged">
<TextBox.ContextMenu>
<ContextMenu Name="ctm" Placement="Relative" 
    Focusable="False" HasDropShadow="False" 
    VerticalOffset="23" HorizontalOffset="0">
    <StackPanel Margin="0" >
        <TextBox Text="testing..." Name="testing"></TextBox>
    </StackPanel>                    
</ContextMenu>
</TextBox.ContextMenu>
</TextBox>

谢谢大家。

c# wpf contextmenu
2个回答
2
投票

解决“颜色”问题的一种方法。您可以覆盖系统颜色以获得您想要的行为。 只需选择您需要覆盖的系统颜色即可。

<ContextMenu>
   <ContextMenu.Resources>
        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="LightBlue"/>
   </ContextMenu.Resources>

编辑:

我在上下文菜单中使用以下内容将选择颜色设置为透明,并将所选项目设置为绿色前景。

<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent"/>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="DarkGreen"/>

0
投票

根据blindmeis的回答,问题是HighlightBrushKey。 要获得其他可能的解决方案,您现在可以查找 wpf 源代码。 对于菜单项,这是

https://github.com/dotnet/wpf/blob/03043efa30c4f13ab630368e9ffa0e9558519f9e/src/Microsoft.DotNet.Wpf/src/Themes/XAML/MenuItem.xaml#L1278

使用以下xaml

<ControlTemplate x:Key="{ComponentResourceKey TypeInTargetAssembly={x:Type MenuItem}, ResourceId=SubmenuItemTemplateKey}"
                     TargetType="{x:Type MenuItem}">
        <Border Name="Bd"
                Background="{TemplateBinding Background}"
                BorderThickness="{TemplateBinding BorderThickness}"
                BorderBrush="{TemplateBinding BorderBrush}">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition MinWidth="17"
                                      Width="Auto"
                                      SharedSizeGroup="MenuItemIconColumnGroup"/>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="Auto"
                                      SharedSizeGroup="MenuItemIGTColumnGroup"/>
                    <ColumnDefinition Width="14"/>
                </Grid.ColumnDefinitions>
                <!-- Glyph -->
                <ContentPresenter x:Name="Icon"
                                  Margin="4,0,6,0"
                                  VerticalAlignment="Center"
                                  ContentSource="Icon"/>
                <Path x:Name="GlyphPanel"
                      Margin="4,0,6,0"
                      Visibility="Hidden"
                      VerticalAlignment="Center"
                      Fill="{TemplateBinding Foreground}"
                      FlowDirection="LeftToRight"
                      Data="{StaticResource Checkmark}"/>
                <ContentPresenter
                                  Grid.Column="1"
                                  ContentSource="Header"
                                  Margin="{TemplateBinding Padding}"
                                  RecognizesAccessKey="True"/>
                <TextBlock x:Name="InputGestureText"
                           Grid.Column="2"
                           Text="{TemplateBinding InputGestureText}"
                           Margin="5,2,0,2"
                           DockPanel.Dock="Right"/>
            </Grid>
        </Border>
        <ControlTemplate.Triggers>
            <Trigger Property="Icon"
                     Value="{x:Null}">
                <Setter TargetName="Icon"
                        Property="Visibility"
                        Value="Collapsed"/>
            </Trigger>
            <Trigger Property="IsChecked"
                     Value="true">
                <Setter TargetName="GlyphPanel"
                        Property="Visibility"
                        Value="Visible"/>
                <Setter TargetName="Icon"
                        Property="Visibility"
                        Value="Collapsed"/>
            </Trigger>
            <Trigger Property="IsHighlighted"
                     Value="true">
                <Setter TargetName="Bd"
                        Property="Background"
                        Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
                <Setter Property="Foreground"
                        Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}"/>
            </Trigger>
            <Trigger Property="IsEnabled"
                     Value="false">
                <Setter Property="Foreground"
                        Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
            </Trigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>

对于您的问题,您可以构建自己的 Controltemplate 并删除触发器和 GlypPanel 等。

© www.soinside.com 2019 - 2024. All rights reserved.