Wpf style resource for foreground color of selected listbox item

BrandonS picture BrandonS · Feb 12, 2009 · Viewed 17.9k times · Source

Background: I am creating a custom listbox that has radio buttons on each listbox item, so essentially it will be a RadioButtonList. The control is created entirely in code. As of right now the control renders and behaves correctly and supports 2 orientations (Horizontal/Vertical). The listbox uses an ItemTemplate that is a StackPanel with a RadioButton and a TextBlock.

So far I have been able to prevent the background color of the item from changing when the item is selected by using a style that sets it's background to transparent.

I would like to also do the same for the foreground color.

Basically, the Selection mode of the ListBox is single, and when an item is selected, I only want it to be reflected by the RadioButton.

I am using the following code to set the ItemContainerStyle:

System.Windows.Style style =  
    new System.Windows.Style(typeof(System.Windows.Controls.ListBoxItem));  

System.Windows.Media.SolidColorBrush brush =  
    new System.Windows.Media.SolidColorBrush(System.Windows.Media.Colors.Transparent);  

style.Resources.Add(System.Windows.SystemColors.HighlightBrushKey, brush);

The TextBlock of my template is created using a System.Windows.FactoryFrameworkElement like this:

System.Windows.FrameworkElementFactory factoryTextBlock =   
    new System.Windows.FrameworkElementFactory(typeof(System.Windows.Controls.TextBlock));
factoryTextBlock.SetBinding(System.Windows.Controls.TextBlock.TextProperty, new System.Windows.Data.Binding("Description"));  
factoryStackPanel.AppendChild(factoryTextBlock);

The FactoryTextBox is then appended to the FactoryStackPanel and is set as the ItemTemplate of the ListBox.

At the moment, I have the background color being set to Transparent when the item is selected. Since the text gets set to white by default, it visually disappears when the item is selected. I am looking for a way to set a color on the foreground of the textblock when it's selected. For now it can be black, but eventually it will reference a font color at a higher level.

Answer

Robert Macnee picture Robert Macnee · Feb 13, 2009

Here's an example using XAML, I'll leave the translation to C# up to you:

<Grid xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:sys="clr-namespace:System;assembly=mscorlib"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Grid.Resources>
        <x:Array x:Key="data" Type="{x:Type sys:String}">
            <sys:String>sphinx</sys:String>
            <sys:String>of</sys:String>
            <sys:String>black</sys:String>
            <sys:String>quartz</sys:String>
        </x:Array>
    </Grid.Resources>
    <ListBox ItemsSource="{StaticResource data}">
        <ListBox.Resources>
            <Style TargetType="{x:Type ListBoxItem}">
                <Style.Triggers>
                    <Trigger Property="IsSelected" Value="True">
                        <Setter Property="Foreground" Value="Pink"/>
                    </Trigger>
                </Style.Triggers>
            </Style>
        </ListBox.Resources>
    </ListBox>
</Grid>