1

UI の残りの部分と一致するように ComboBoxes のスタイルを設定しようとしましたが、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 TemplateまたはBlendを使用すると、ComboBox templpate がどのようなものかの基本的なアイデアを得ることができます。

同じスタイルを使用してテンプレートをオーバーライドできます。

<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 Designer から既定のテンプレートのコピーを取得し、ComboBoxReadonlyToggleButton スタイルで ButtonChrome セクションをコメント アウトして、Border に置き換えることで、この動作をオーバーライドできます。これは私が解決策を見つけたサイトへのリンクです - 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 に答える