Background: I am creating a custom list that has radio buttons on each element of the list, so essentially it will be a RadioButtonList. The control is fully created in code. At the moment, the control displays and behaves correctly and supports 2 orientations (horizontal / vertical). The list uses an ItemTemplate, which is a StackPanel with RadioButton and TextBlock.
So far, I have managed to prevent the background color of the element from changing when I select the element, using a style that sets it to transparent.
I would also like to do the same for foreground color.
Basically, the ListBox selection mode is single, and when an item is selected, I want it to be reflected in RadioButton.
I use the following code to set 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 text block of my template is created using System.Windows.FactoryFrameworkElement as follows:
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);
Then, the FactoryTextBox is added to the FactoryStackPanel and set as an ItemTemplate in the ListBox.
At the moment, my background color is set to Transparent when the item is selected. Since the text turns white by default, it visually disappears when an item is selected. I am looking for a way to set the color in the foreground of a text block when it is selected. It may be black at the moment, but ultimately it will refer to the font color at a higher level.