C# WPF ComboBox 鼠标悬停在颜色上 [英] C# WPF ComboBox Mouse over color

查看:20
本文介绍了C# WPF ComboBox 鼠标悬停在颜色上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当鼠标悬停在我的 ComboBox 上时,我的 ComboBox 背景出现了一对糟糕的蓝色/浅蓝色.我在这里尝试了解决方案:ComboBox Mouse over color, WPF 组合框鼠标悬停如何在鼠标悬停时设置 ComboBox 背景的样式?切换按钮上的 WPF 组合框默认悬停颜色,但它没有改变任何东西,悬停时我仍然获得默认颜色.

When the mouse hover over my ComboBox, I get an awful blue/lightblue pair of color for the backgroung of my ComboBox. I tried the solutions here :ComboBox Mouse over color, WPF Combobox Mouse Over, How to style ComboBox Background on Mouse Hover? or WPF combobox default hover color on togglebutton, but it doesn't change anything, I still get the default colors while hovering.

有什么建议吗?

先谢谢大家,德马西亚多.

Thank you all in advance, Demasiado.

这是 XAML 代码:

Here is the XAML code :

<Window x:Class="Homepage.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:sys="clr-namespace:System;assembly=mscorlib" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    <Window.Resources>
        <Storyboard x:Key="TileZoomIn">
            <ThicknessAnimation Storyboard.TargetProperty="Margin" From="10" To="1" Duration="0:0:0.1"/>
        </Storyboard>
        <Storyboard x:Key="TileZoomOut">
            <ThicknessAnimation Storyboard.TargetProperty="Margin" From="1" To="10" Duration="0:0:0.1"/>
        </Storyboard>
        <DropShadowEffect x:Key="DropShadowEffect" BlurRadius="20" Opacity="1" ShadowDepth="0" Color="White"/>
    </Window.Resources>

    <Grid ShowGridLines="True">
        <ComboBox Name="comboBoxTRIG" FontSize="40" Width="210" Height="98" HorizontalAlignment="Left" HorizontalContentAlignment="Center" Margin="40,-180,0,256" Background="Transparent" BorderBrush="Transparent" Foreground="White" BorderThickness="0">
            <ComboBox Margin="25" Width="130" Height="50">
                <ComboBox.Resources>
                    <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Green"/>
                </ComboBox.Resources>
            </ComboBox>
        </ComboBox>
    </Grid>
</Window>

推荐答案

您的问题来自 ToggleButton 模板中的 ButtonChrome.从 ToggleButton 中删除它.

Your problem arises from ButtonChrome in the ToggleButton's template. Remove it from the ToggleButton.

   ComboBox -> ToggleButton -> ButtonChrome 

步骤:

1) 打开 Expression Blend 并编辑 ComboBox 样式的副本,这将为您提供 ComboBox 的样式 + 它的模板以及所有它的 TemplateParts ,其中包括有问题的 ToggleButton.

1) Open Expression Blend and edit a copy of ComboBox's Style , This will give you the Style of the ComboBox + it's Template and all it's TemplateParts , Among them is the problematic ToggleButton.

2) 找到 ToggleButton,它的 Style 称为ComboBoxReadonlyToggleButton".

2) Locate the ToggleButton and it's Style called "ComboBoxReadonlyToggleButton" .

3) 在ComboBoxReadonlyToggleButton"中,将 Themes:ButtonChrome 替换为 Border(如下面的第 3 块代码所示.)

3) In "ComboBoxReadonlyToggleButton" replace the Themes:ButtonChrome with a Border (like shown in the 3'rd block of code below.)

组合框的默认模板(简化版!):

<ControlTemplate TargetType="{x:Type ComboBox}">
    <Grid x:Name="MainGrid" SnapsToDevicePixels="true">                                 
        <Popup x:Name="PART_Popup">
            .....
        </Popup>
        <ToggleButton  Style="{StaticResource ComboBoxReadonlyToggleButton}"/>
        <ContentPresenter ... />
    </Grid>                     
</ControlTemplate>

切换按钮样式 + 模板(简化!).

<Style x:Key="ComboBoxReadonlyToggleButton" TargetType="{x:Type ToggleButton}">     
    <Setter Property="Background" Value="Transparent"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ToggleButton}">
                <Themes:ButtonChrome x:Name="Chrome" ....>
                    <Grid>
                        <Path x:Name="Arrow" />
                    </Grid>
                </Themes:ButtonChrome>  
             </ControlTemplate>             
        </Setter.Value>
    </Setter>
</Style> 

您需要做的是覆盖默认的 ComboBox 模板并通过将 ButtonChrome 替换为 Border 来编辑切换按钮的样式:

What you need to do is to override the default ComboBox template and edit the toggle button's style by replacing ButtonChrome with a Border :

 <Setter Property="Template">
     <Setter.Value>
        <ControlTemplate TargetType="{x:Type ToggleButton}">
              <Border x:Name="Chrome" BorderBrush="{TemplateBinding BorderBrush}" Background="{TemplateBinding Background}" SnapsToDevicePixels="true">
                <Grid>
                    <Path x:Name="Arrow" />
                </Grid>
            </Border>               
        </ControlTemplate>
    </Setter.Value>
</Setter>

这篇关于C# WPF ComboBox 鼠标悬停在颜色上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆