2008-10-30 6 views
1

나머지 ComboBox를 스타일 화하려고했지만 IsMouseOver 강조 표시에 문제가 있습니다. 그것은 두 번째로 지정한 색상으로 강조 표시 한 다음 기본 색상으로 되돌아갑니다. 멋진 효과는 있지만 내가하려는 것은 아닙니다. 내 스타일은 다음과 같습니다.MouseOver 강조 표시 스타일이 1 초 후에 기본값으로 돌아옴 (에어로 인해 발생)

<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 Template 또는 Blend을 사용하여 ComboBox 템플리트의 기본 아이디어를 얻을 수 있습니다.

동일한 스타일을 사용하여 템플릿을 덮어 쓸 수 있습니다.

<Style TargetType="{x:Type ComboBox}"> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type ComboBox}"> 
       <!-- Template Here --> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 
+0

감사합니다. 나는 두려웠습니다. 지금 템플릿을 만드는 중입니다. 템플릿 표시 링크는 매우 유용했습니다. –

+1

템플릿이 아닌 OnRenderMouseOverChanged에 문제가 실제로있는 경우 어떻게 템플릿을 변경 하시겠습니까? 나는이 똑같은 문제가 있으며 그것을 고치는 방법에 관해서 나에게 불분명하다. – skybluecodeflier

0

당신은 WPF 비주얼 스튜디오 디자이너에서 기본 템플릿의 사본을 얻어서이 동작을 재정의하고 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 --> 

되고 또한 DarkOrange에 배경색을 변경하는 몇 가지 코드를 추가 - 이 코드는 ControlTemplate에 들어갔다 (섹션에서) ComboBox 용 스타일.

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