设置ComboBox的背景颜色MouseOver在样式中突出显示的项目 [英] Set background color of ComboBox MouseOver highlighted item in the style

查看:109
本文介绍了设置ComboBox的背景颜色MouseOver在样式中突出显示的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要解决此问题中提到的相同问题:设置ComboBox所选项目亮点颜色:设置突出显示项目的背景。

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>

我不知道如何实现这一点。试图修改Resources或SolidColorBrush属性,但没有工作。

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

推荐答案

如果你想要< c $ c> Style 的 SolidColorBrush 应用于所有 ComboBox ,然后将其移动到 Style.Resources 部分:

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>

这篇关于设置ComboBox的背景颜色MouseOver在样式中突出显示的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆