WPF-上下文菜单-如何禁用鼠标或焦点的背景变化

huwehgph  于 11个月前  发布在  其他
关注(0)|答案(2)|浏览(123)

这是我在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>


感谢所有人。

5ssjco0h

5ssjco0h1#

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

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

字符串
编辑:
我使用下面的内容作为我的contextmenu,将selectioncolor设置为透明,将选中的项目设置为绿色前景。

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

ecbunoof

ecbunoof2#

作为blindmei的回答,问题是HighlightBrushKey。要获得其他可能的解决方案,您可以现在查找wpf源代码。对于menuitem,这是
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>

字符串
对于你的问题,你可以建立你自己的控制模板,并删除触发器和GlypPanel等。

相关问题