tags:

views:

839

answers:

1

I have an issue with ComboBox highlighting which shows black text on a blue background, when the text for highlighting should be white.

I have examples of ComboBox which use ComboBoxItems where the Content is a string. The combobox in these cases behave as expected - when the combobox is dropped down if you highlight an item it displays white text on a blue blackground.

However I have an example ComboBox where the Content of each ComboBoxItem is a Grid (the Grid contains 2 columns - the first which contains text and the second a line - it is a line thickness combobox). In this case when the combobox is dropped down, if you highlight an item it displays black text on a blue background instead of white text. Note: even if i remove the line part and so just have one column containing text I still see the issue.

The nearest I have come to solving the issue is to add a resource to the combobox for SystemColors.HighlightBrushKey and SystemColors.HighlightTextBrushKey where I set the colour of the brush. However, SystemColors.HighlightBrushKey does correctly change the back colour of the highlight (this is not what I want though), and when I try to use SystemColors.HighlightTextBrushKey which I thought would change the text colour of a highlighted item nothing happens (the colour does not change).

Example edited code:

var combo = new ComboBox();

Func<double, object> build = d =>
{
    var grid = new Grid();
    grid.ColumnDefinitions.Add(new ColumnDefinition {Width = GridLength.Auto});

    var label = new Label {Content = d};
    grid.Children.Add(label);
    Grid.SetColumn(label, 0);

    var comboBoxItem = new ComboBoxItem {Content = grid, Tag = d};
    return comboBoxItem;
};

combo.Items.Add(build(0.5));
combo.Items.Add(build(1));
combo.Items.Add(build(2));
...

I have tried:

combo.Resources.Add(SystemColors.HighlightBrushKey, Brushes.Green); // this does set the back to green (but is not what I want)
combo.Resources.Add(SystemColors.HighlightTextBrushKey, Brushes.White); // this does not change the text colour to white it stays as black

Any help appreciated, thanks.

+2  A: 

The problem is that you're using a Label control, which defines a fixed Black Foreground which then doesn't inherit the ComboBoxItem's color that changes based on highlighted state. If you aren't doing anything Label specific (uses are rare), consider switching it to a TextBlock. If you need to keep the Label you can do something like this to explicitly force it to inherit:

<ComboBox x:Name="MyComboBox">
    <ComboBox.Resources>
        <Style TargetType="{x:Type Label}">
            <Setter Property="Foreground" Value="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ComboBoxItem}}, Path=Foreground}" />
        </Style>
    </ComboBox.Resources>
</ComboBox>

or if you prefer in code you can set them individually:

...
var label = new Label { Content = d };
label.SetBinding(ForegroundProperty, new Binding("Foreground") { RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor, typeof(ComboBoxItem), 1) });
grid.Children.Add(label);
...
John Bowen
Thanks for the answer, I can change to TextBlock and that works fine.
TheBlackKnight