代码之家  ›  专栏  ›  技术社区  ›  Bryan Anderson

鼠标悬停突出显示样式一秒钟后返回默认值(由Aero引起?)

  •  1
  • Bryan Anderson  · 技术社区  · 16 年前

    我试图对组合框进行样式设置,以匹配UI的其余部分,但IsmouseOver突出显示有问题。它用我指定的颜色亮显一秒钟,然后淡入默认颜色,有点酷的效果,但不是我想要的。这是我的风格:

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

    我该怎么做才能保持背景色?

    2 回复  |  直到 9 年前
        1
  •  4
  •   Todd White    16 年前

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

    即使这是一个很大的工作,至少对于ComboBox来说,您可能需要重写ComboBox的默认模板。您可以通过使用 Show Me The Template Blend .

    可以使用相同的样式替代模板。

    <Style TargetType="{x:Type ComboBox}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ComboBox}">
                    <!-- Template Here -->
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    
        2
  •  0
  •   user1290865    9 年前

    您可以通过从WPF Visual Studio设计器中获取默认模板的副本,然后在ComboBoxReadOnlyToggleButton样式注释中去掉ButtonChrome部分,并将其替换为边框来覆盖此行为。这是我找到解决方案的站点的链接- http://www.scriptscoop.net/t/d346cf01d844/c-c-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 -->
    

    我还添加了一些代码将背景色改为深橙色。- 此代码进入了组合框样式的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 -->