1

我试图设置我的 ComboBoxes 的样式以匹配 UI 的其余部分,但我遇到了 IsMouseOver 突出显示的问题。它以我指定的颜色突出显示一秒钟,然后淡出默认颜色,这是一种很酷的效果,但不是我想要的。这是我的风格:

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

我该怎么做才能使背景颜色保持不变?

4

2 回答 2

4

问题确实是由于 ComboBox 的默认模板。如果您使用Reflector打开 PresentationFramework.Aero 程序集,您可以查看 ButtonChrome 类。有一个名为 OnRenderMouseOverChanged 的​​方法可以隐藏红色背景。

尽管工作量很大,但至少对于 ComboBox,您可能希望覆盖 ComboBox 的默认模板。您可以通过使用Show Me The TemplateBlend来了解 ComboBox 模板是什么样的基本概念。

您可以使用相同的样式来覆盖模板。

<Style TargetType="{x:Type ComboBox}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ComboBox}">
                <!-- Template Here -->
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
于 2008-10-31T02:29:42.970 回答
0

您可以通过从 WPF Visual Studio 设计器获取默认模板的副本来覆盖此行为,然后在 ComboBoxReadonlyToggleButton 样式中注释掉 ButtonChrome 部分并将其替换为边框。这是我找到解决方案的网站的链接 - http://www.scriptscoop.net/t/d346cf01d844/cc-wpf-combobox-mouse-over-color.html

这是我的代码片段

<Style x:Key="ComboBoxReadonlyToggleButton" TargetType="{x:Type ToggleButton}">
  <Setter Property="OverridesDefaultStyle" Value="true"/>
  <Setter Property="IsTabStop" Value="false"/>
  <Setter Property="Focusable" Value="false"/>
  <Setter Property="ClickMode" Value="Press"/>
  <Setter Property="Background" Value="Transparent"/>
  <Setter Property="Template">
    <Setter.Value>
      <ControlTemplate TargetType="{x:Type ToggleButton}">
        <!-- Replace the ButtonChrome - this eliminated the following
             problem:  When the mouse was moved over the ComboBox
             the color would change to the color defined in ___ but 
             then would  
             immediately change to the default Aero blue
             gradient background of 2 powder blue colors  - 
             Had to comment out the          
             below code and replace it as shown
             <Themes:ButtonChrome x:Name="Chrome" BorderBrush="                 {TemplateBinding BorderBrush}" Background="{TemplateBinding Background}" RenderMouseOver="{TemplateBinding IsMouseOver}" RenderPressed="{TemplateBinding IsPressed}" SnapsToDevicePixels="true">
               <Grid HorizontalAlignment="Right" Width="{DynamicResource {x:Static SystemParameters.VerticalScrollBarWidthKey}}">
                 <Path x:Name="Arrow" Data="{StaticResource DownArrowGeometry}" Fill="Black" HorizontalAlignment="Center" Margin="3,1,0,0" VerticalAlignment="Center"/>
               </Grid>
             </Themes:ButtonChrome>-->

         <!-- Here is the code to replace the ButtonChrome code -->
         <Border x:Name="Chrome" BorderBrush="{TemplateBinding BorderBrush}" Background="{TemplateBinding Background}" SnapsToDevicePixels="true">
           <Grid HorizontalAlignment="Right" Width="{DynamicResource {x:Static SystemParameters.VerticalScrollBarWidthKey}}">
             <Path x:Name="Arrow" Data="{StaticResource DownArrowGeometry}" Fill="Black" HorizontalAlignment="Center" Margin="3,1,0,0" VerticalAlignment="Center"/>
           </Grid>
         </Border>
       <!-- End of code to replace the Button Chrome -->

我还添加了一些代码来将背景颜色更改为 DarkOrange - 此代码进入 ComboBox 样式的 ControlTemplate(在该部分中)。

<!-- Hover Code - Code that was added to change the ComboBox background 
     color when the use hovers over it with the mouse -->
<Trigger Property="IsMouseOver" Value="True">
   <Setter Property="Background" Value="DarkOrange"></Setter>
</Trigger>
<!-- Hover Code - End -->
于 2015-12-30T16:34:36.913 回答