Evans
Evans

Reputation: 1599

Set background color of ComboBox MouseOver highlighted item in the style

I want to achive the same thing asked in this question: Set ComboBox selected item highlight color: set the background of the highlighted item.

However, instead of adding this to every combobox:

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

I want to add this to the style i have for every combobox. This is my combobox style:

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

I have no idea about how to achieve this. Tried to modify a "Resources" or "SolidColorBrush" property, but didn't work.

Upvotes: 1

Views: 2152

Answers (1)

Sheridan
Sheridan

Reputation: 69959

If you want the SolidColorBrush to apply to all of the ComboBoxes affected by your Style, then just move it to your Style.Resources section:

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

Upvotes: 3

Related Questions