Stop highlighting selected item WPF ComboBox

2019-04-11 22:16发布

I'm currently working on a WPF UI and I have a combobox on my window. So I want the user to be able to select an item from this combobox but when it is selected I don't want it to be highlighted in the default blue colour.

I assume there is some way to stop this in XAML but I have not been able to find the solution so far.

Thanks.

P.S. I don't have access to Expression Blend so if anyone suggests a solution could it be in XAML

EDIT: Just to make it clearer I by selected I mean once you have selected a value and the SelectionChanged event has fired and the item is displayed in the combobox and the combo box is highlighted like so: alt text

2条回答
干净又极端
2楼-- · 2019-04-11 22:41

Did you try to just set the ComboBox.Background property ?

查看更多
兄弟一词,经得起流年.
3楼-- · 2019-04-11 23:03

You need to set appearance of your selection via styles.

    <Window.Resources>
    <Style TargetType="{x:Type ComboBoxItem}">
        <Setter Property="Control.Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ComboBoxItem}">
                    <Border Background="{TemplateBinding Background}">
                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition />
                                <RowDefinition />
                            </Grid.RowDefinitions>
                            <Border Margin="2" Grid.Row="0" Background="Azure" />
                            <ContentPresenter />
                        </Grid>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="Background" Value="Green" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Window.Resources>

This style will be automatically applied to any ComboBoxes within the window:

<StackPanel>
    <ComboBox>
        <ComboBoxItem>111</ComboBoxItem>
        <ComboBoxItem>222</ComboBoxItem>
        <ComboBoxItem>333</ComboBoxItem>
        <ComboBoxItem>444</ComboBoxItem>
        <ComboBoxItem>555</ComboBoxItem>
    </ComboBox>
</StackPanel>

You will see it as follows:

http://i.stack.imgur.com/b4pDo.png

UPD: In order to remove highlighting from selected item you need to modify system brushes which are actually used for these purposes. Just add two extra styles:

    <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent"/>
    <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Black"/>  
查看更多
登录 后发表回答