'How to style ContentPresenter in ComboBox?
I'm currently styling ComboBox to look like one in Visual Studio (along with color theme). I've done most of the styling, but stopped at the ContentPresenter displaying the currently chosen object.
The piece of XAML looks like the following:
<ContentPresenter Margin="2" IsHitTestVisible="False" VerticalAlignment="Center" HorizontalAlignment="Stretch"
Name="ContentSite"
ContentTemplate="{TemplateBinding ComboBox.SelectionBoxItemTemplate}"
Content="{TemplateBinding ComboBox.SelectionBoxItem}" />
The problem is, that the default SelectionBoxItemTemplate seems to ignore ComboBox's Foreground value:
<ComboBox Margin="4" SelectedIndex="0" Foreground="Red">
<ComboBoxItem>First</ComboBoxItem>
<ComboBoxItem>Second</ComboBoxItem>
<ComboBoxItem>Third</ComboBoxItem>
</ComboBox>

The first one is non-editable, SelectionBoxItemTemplate kicks in and forces set the color of the text to black (even though Foreground is set manually to red and in style to another non-black color).
How can I pass the current foreground color to the ContentPresenter? If I can't, where can I find working replacement for the SelectionBoxItemTemplate? (working, in terms, that it'll work correctly for all item types in the ComboBox)
Solution 1:[1]
I don't think that it does ignore the Foreground colour. If you add a (normal) ComboBox control like the following to your XAML...:
<ComboBox Foreground="Red">
<ComboBoxItem>One</ComboBoxItem>
<ComboBoxItem>Two</ComboBoxItem>
<ComboBoxItem>Three</ComboBoxItem>
<ComboBoxItem>Four</ComboBoxItem>
</ComboBox>
... you will see that setting the Foreground does change the selected item text. I think that perhaps your problem lies in your ControlTemplate. Now I can't be 100% sure, but I would have thought that your ContentPresenter should look like this instead:
<ContentPresenter Margin="2" IsHitTestVisible="False" VerticalAlignment="Center"
HorizontalAlignment="Stretch" Name="ContentSite" ContentTemplate="{TemplateBinding
SelectionBoxItemTemplate}" Content="{TemplateBinding SelectionBoxItem}" />
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Sheridan |
