Wpf – ListBox Selected Text Color

visual studiovisual studio 2010wpfxaml

Second question of the day but hopefully simple, I just need to change the foreground color of the selected item in my listbox control.

I have the following style (Which changes the Background on selection but not the foreground color):

    <Style TargetType="ListBoxItem" x:Key="PinnedListBoxItem">
    <Style.Resources>
        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="#FF9CC164"/>
        <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="#FF9CC164"/>
        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="White"/>
    </Style.Resources>
</Style>

This also doesn't work

    <Style TargetType="ListBoxItem" x:Key="PinnedListBoxItem">
    <Style.Resources>
        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="#FF9CC164"/>
        <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="#FF9CC164"/>
    </Style.Resources>
    <Style.Triggers>
        <Trigger Property="IsSelected" Value="True">
            <Setter Property="Foreground" Value="White"/>
        </Trigger>
    </Style.Triggers>
</Style>

I have the following ListBox:

                <ListBox Style="{StaticResource PinnedList}" Height="Auto" MaxHeight="200" Visibility="Hidden" HorizontalAlignment="Left" Margin="-8,45,0,0" SelectionChanged="ui_lsbPinnedORGs_SelectionChanged"
                     SelectedItem="{Binding Path=SelectedPinnedORGsViewModel, UpdateSourceTrigger=PropertyChanged, NotifyOnSourceUpdated=True, NotifyOnTargetUpdated=True, Mode=TwoWay}"
                     SelectionMode="Single" ItemsSource="{Binding Path=ORGViewModels, UpdateSourceTrigger=PropertyChanged, NotifyOnSourceUpdated=True, NotifyOnTargetUpdated=True, Mode=TwoWay}"
                     ScrollViewer.HorizontalScrollBarVisibility="Disabled" ScrollViewer.VerticalScrollBarVisibility="Auto" MouseEnter="ui_lsbPinnedORGs_MouseEnter"
                     VerticalAlignment="Top" Width="158" Name="ui_lsbPinnedORGs" ItemContainerStyle="{StaticResource PinnedListBoxItem}">
                <ListBox.ItemTemplate>
                    <HierarchicalDataTemplate>
                        <Label Content="{Binding Path=ORG.Acronym, UpdateSourceTrigger=PropertyChanged, NotifyOnSourceUpdated=True}" />
                    </HierarchicalDataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>

Best Answer

You should use a basic trigger like:

<Style TargetType="ListBoxItem">
  ...
  <Style.Triggers>
     <Trigger Property="IsSelected" Value="True">
        <Setter Property="Foreground" Value="Color"/>
     </Trigger>
  </Style.Triggers>
</Style>

UPDATE

With the current template:

<ListBox.ItemTemplate>
   <HierarchicalDataTemplate>
      <Label Content="{Binding}" Foreground="{Binding RelativeSource={RelativeSource AncestorType=ListBoxItem, Mode=FindAncestor}, Path=Foreground}" />
   </HierarchicalDataTemplate>
</ListBox.ItemTemplate>
Related Topic