在样式中设置ComboBox MouseOver高亮显示项目的背景颜色

时间:2014-02-14 16:11:14

标签: c# wpf combobox

我想在这个问题中提出同样的问题:Set ComboBox selected item highlight color:设置突出显示项目的背景。

但是,不要将其添加到每个组合框中:

<ComboBox  ... >
    <ComboBox.Resources>
        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="#FF745005"/>
    </ComboBox.Resources>
    ...
</ComboBox>

我想将此添加到我对每个组合框的样式中。这是我的组合框样式:

<Style TargetType="ComboBox">
    <Setter Property="VerticalAlignment" Value="Center" />
    <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto" />
    <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto" />
    <Setter Property="Margin" Value="2" />
    <Setter Property="MinHeight" Value="20" />


        <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ComboBox">
                <Grid>
                    <VisualStateManager.VisualStateGroups>
                        <VisualStateGroup x:Name="CommonStates">
                            <VisualState x:Name="Normal"/>
                            <VisualState x:Name="MouseOver"/>
                            <VisualState x:Name="Disabled"/>
                        </VisualStateGroup>
                    </VisualStateManager.VisualStateGroups>
                    <ToggleButton x:Name="ToggleButton" Grid.Column="2" ClickMode="Press" 
                        Focusable="false"
                        IsChecked="{Binding IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"
                        Template="{StaticResource ComboBoxToggleButton}"/>

                    <ContentPresenter Margin="3,3,23,3" 
                        Content="{TemplateBinding SelectionBoxItem}"
                        ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}"
                        ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}"
                        HorizontalAlignment="Left" IsHitTestVisible="False" x:Name="ContentSite"
                        VerticalAlignment="Center" />

                    <TextBox Style="{x:Null}" 
                        x:Name="PART_EditableTextBox" 
                        Margin="3,3,23,3" 
                        Background="Transparent"
                        Focusable="True" 
                        HorizontalAlignment="Left" 
                        IsReadOnly="True"
                        Template="{StaticResource ComboBoxTextBox}" 
                        VerticalAlignment="Center" 
                        Visibility="Hidden"
                        Text="{TemplateBinding Text}"
                             />

                    <Popup 
                        AllowsTransparency="True" 
                        Focusable="False" 
                        IsOpen="{TemplateBinding IsDropDownOpen}"
                        x:Name="Popup" 
                        Placement="Bottom" 
                        PopupAnimation="Fade">
                        <Grid MaxHeight="{TemplateBinding MaxDropDownHeight}" MinWidth="{TemplateBinding ActualWidth}" x:Name="DropDown" 
                            SnapsToDevicePixels="True">
                            <Border x:Name="DropDownBorder" 
                                    Background="White" 
                                    BorderBrush="{StaticResource BorderBrush}" 
                                    BorderThickness="1" 
                                    CornerRadius="0" />
                            <ScrollViewer Margin="2" 
                                SnapsToDevicePixels="True"
                                Background="{StaticResource BackgroundBrush}">
                                <StackPanel 
                                    KeyboardNavigation.DirectionalNavigation="Contained" 
                                    IsItemsHost="True" 
                                    TextBlock.Foreground="Black"
                                    />
                            </ScrollViewer>
                        </Grid>
                    </Popup>
                </Grid>
                <ControlTemplate.Triggers>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Style.Triggers>
    </Style.Triggers>
</Style>

我不知道如何实现这一目标。试图修改“资源”或“SolidColorBrush”属性,但没有用。

1 个答案:

答案 0 :(得分:3)

如果您希望SolidColorBrush适用于受ComboBox影响的所有Style,请将其移至Style.Resources部分:

<Style TargetType="ComboBox">
    <Style.Resources>
        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="#FF745005"/>
    </Style.Resources>
    <Setter Property="Template">
        ...
    </Setter>
</Style>