views:

89

answers:

2

Hi,

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

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.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"/>  
Bashir Magomedov
See also: http://stackoverflow.com/questions/794792/wpf-listbox-selection-color
Bashir Magomedov
Thanks that was helpful, but what I meant was when you select an item (say 333) and the drop down list closes and the selected item is shown in the combobox where before it was blank, I don't want it to be highlighted like it is by default.
Michael Lockwood
Ok :) Please check update in the answer.
Bashir Magomedov
A: 

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

ikirachen
That changes the background of the default look, it doesn't stop it from highlighting when focused
Michael Lockwood