繁体   English   中英

C#WPF ComboBox鼠标颜色

[英]C# WPF ComboBox Mouse over color

当鼠标悬停在我的ComboBox上时,我的ComboBox的背景会产生一对可怕的蓝色/浅蓝色。 我在这里尝试了解决方案: ComboBox鼠标颜色WPF Combobox鼠标结束如何在鼠标悬停上设置ComboBox背景样式? 或者WPF组合框在togglebutton上默认悬停颜色 ,但它不会改变任何东西,我仍然在悬停时获得默认颜色。

有什么建议 ?

感谢所有人,Demasiado。

这是XAML代码:

<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中删除它。

   ComboBox -> ToggleButton -> ButtonChrome 

脚步 :

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

2)找到ToggleButton,它的样式名为“ComboBoxReadonlyToggleButton”。

3)在“ComboBoxReadonlyToggleButton” 中用 边框替换主题:ButtonChrome (如下面第3段代码所示)

ComboBox的默认模板(Simplified!):

<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模板,并通过将BorderChrome替换为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>

您可以在ComboBox范围内覆盖SystemColors.HighlightBrushKey

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

样品

整个XAML可以是这样的:

<Grid>
    <ComboBox Margin="25" Width="130" Height="50">
        <ComboBox.Resources>
            <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Green"/>
        </ComboBox.Resources>
        <ComboBox.ItemsSource>
            <Binding Path="Collection" Source="{StaticResource viewmodel}"/>
        </ComboBox.ItemsSource>
    </ComboBox>
</Grid>

您的评论的后续内容:

忽略viewmodel和我的ItemSource你应该使用自己的,这只是为了演示。

至于你的评论

我无法得到任何工作

我建议您创建一个新项目并仅测试此XAML(当然还有您的ItemSource)并查看是否可以获得所需的结果。 当你得到它时,你可以移动到你的真实项目,看看样式在哪里变化,问题出在哪里。

编辑#2:

为了改变ToggleButton的颜色,我认为最好覆盖整个ComboBox样式。

在此输入图像描述

我用过

 <ControlTemplate x:Key="ComboBoxToggleButton"
                 TargetType="{x:Type ToggleButton}">

其余的代码和样式取自这里

我建议你也阅读这篇文章

在我的测试项目的ComboBox中没有项目。 我只是将鼠标悬停在它上面,整个控件都是蓝色的。 这是.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApplication1
{
    /// <summary>
    /// Logique d'interaction pour MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    }
}

这是xaml:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <ComboBox Name="comboBoxTRIG" FontSize="40" Width="210" Height="98" HorizontalAlignment="Left" HorizontalContentAlignment="Center" Margin="42,38,0,184" Background="Red" BorderBrush="Transparent" Foreground="White" BorderThickness="0">
            <ComboBox Width="130" Height="50">
                <ComboBox.Resources>
                    <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Green"/>
                </ComboBox.Resources>
            </ComboBox>
        </ComboBox>

    </Grid>
</Window>

在窗口资源中重新定义comboxItem模板

<Style TargetType="{x:Type ComboBoxItem}"
               BasedOn="{StaticResource {x:Type ComboBoxItem}}">
            <Setter Property="Template">
                <Setter.Value>

                    <ControlTemplate TargetType="{x:Type ComboBoxItem}">
                        <Border Background="{TemplateBinding Background}"
                                BorderBrush="{TemplateBinding BorderBrush}"
                                BorderThickness="{TemplateBinding BorderThickness}"
                                Margin="{TemplateBinding Margin}"
                                Padding="{TemplateBinding Padding}">
                            <ContentPresenter Margin="{TemplateBinding Margin}"
                                                  VerticalAlignment="{TemplateBinding VerticalAlignment}"
                                                  HorizontalAlignment="{TemplateBinding HorizontalAlignment}"/>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>

            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Background" 
                            Value="Red"/>
                </Trigger>
            </Style.Triggers>

        </Style>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM