2008-10-30 21 views
1

我想嘗試樣式我的ComboBoxes以匹配其餘的用戶界面,但我有IsMouseOver突出顯示的問題。它突出顯示我指定的顏色一秒鐘,然後淡出回到默認顏色,這是一種很酷的效果,但不是我要做的。這是我的風格:MouseOver突出顯示一秒鐘後返回到默認樣式(由Aero?引起)

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

我能做些什麼來使背景色留下來?

回答

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> 
+0

謝謝,我很害怕這個。我正在努力建立我的模板。 「顯示我的模板」鏈接非常有幫助。 – 2008-10-31 19:48:59

0

您可以通過從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 --> 

我還添加了一些代碼的背景顏色更改爲深橙 - 此代碼走進控件模板(在該部分)爲組合框的樣式。

<!-- 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 -->