Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing WPF Listbox SelectedItem text color and highlight/background Color using C#

Tags:

c#

wpf

listbox

I am trying to change the highlighted(selected) color and the highlighted text color of a wpf listbox at runtime. I have tried creating a style and applying it as follows:

    Style s = new Style(typeof(ListBox));
    s.Resources.Add(SystemColors.HighlightBrushKey, Setting.ListSelectedColor);
    s.Resources.Add(SystemColors.HighlightTextBrushKey, Setting.ListSelectedTextColor);
    lstGames.Style = s;

But this seems to do nothing. Is there any way to achieve this?

EDIT:

Per suggestions, I tried using DynamicResources to achieve this, but so far this has not been successful either. My code for this:

DYNAMICRESOURCES

<UserControl.Resources>
    <Color x:Key="ListTextSelectedColor"/>
    <Color x:Key="ListSelectedColor"/>
</UserControl.Resources>

LISTBOX

        <ListBox ScrollViewer.HorizontalScrollBarVisibility="Hidden" ScrollViewer.VerticalScrollBarVisibility="Hidden" 
             Name="lstGames" Margin="20" Grid.Row="2" Grid.Column="2" 
             SelectionChanged="lstGames_SelectionChanged" Grid.RowSpan="2" Grid.ColumnSpan="2" 
             Background="{x:Null}" BorderBrush="{x:Null}" SelectionMode="Single"
             FontSize="18" FontFamily="OCR A Extended">
        <Style TargetType="ListBox">
            <Style.Resources>
                <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="{DynamicResource ListSelectedColor}"/>
                <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="{DynamicResource ListSelectedColor}"/>
                <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="{DynamicResource ListTextSelectedColor}"/>
                <SolidColorBrush x:Key="{x:Static SystemColors.ControlTextBrushKey}" Color="{DynamicResource ListTextSelectedColor}"/>
            </Style.Resources>
        </Style>
    </ListBox>

APPLYING RESOURCES IN C#

this.Resources["ListSelectedColor"] = SETING.ListSelectedColor.Color;
this.Resources["ListTextSelectedColor"] = SETTING.ListSelectedTextColor.Color;
like image 688
dingdangdowney Avatar asked Feb 24 '15 01:02

dingdangdowney


2 Answers

Solution:

<Window x:Class="ListBoxStyle.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:src="clr-namespace:ListBoxStyle"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <Style x:Key="_ListBoxItemStyle" TargetType="ListBoxItem">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="ListBoxItem">
                        <Border Name="_Border"
                                Padding="2"
                                SnapsToDevicePixels="true">
                            <ContentPresenter />
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsSelected" Value="true">
                                <Setter TargetName="_Border" Property="Background" Value="Yellow"/>
                                <Setter Property="Foreground" Value="Red"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>
    <Grid>
        <ListBox ItemContainerStyle="{DynamicResource _ListBoxItemStyle}"
                 Width="200" Height="250"
                 ScrollViewer.VerticalScrollBarVisibility="Auto"
                 ScrollViewer.HorizontalScrollBarVisibility="Auto">
            <ListBoxItem>Hello</ListBoxItem>
            <ListBoxItem>Hi</ListBoxItem>
        </ListBox>
    </Grid>
</Window>
like image 139
Vinkal Avatar answered Sep 23 '22 02:09

Vinkal


Thanks to Vinkal and failedprogramming, I got everything working beautifully. I created the following Resources:

<UserControl.Resources>
        <SolidColorBrush x:Key="ListTextSelectedColor" x:Shared="False"/>
        <SolidColorBrush x:Key="ListSelectedColor" x:Shared="False"/>
        <Style x:Key="_ListBoxItemStyle" TargetType="ListBoxItem">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="ListBoxItem">
                        <Border Name="_Border"
                                Padding="2"
                                SnapsToDevicePixels="true">
                            <ContentPresenter />
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsSelected" Value="true">
                                <Setter TargetName="_Border" Property="Background" Value="{DynamicResource ResourceKey=ListSelectedColor}"/>
                                <Setter Property="Foreground" Value="{DynamicResource ResourceKey=ListTextSelectedColor}"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </UserControl.Resources>

And then applied the style to my listbox with:

ItemContainerStyle="{DynamicResource ResourceKey=_ListBoxItemStyle}"

And finally, change the solidcolorbrush resources (therefore changing the setter values) in my C# code by doing the following:

    this.Resources["ListSelectedColor"] = EmulatorPage.ListSelectedColor;
    this.Resources["ListTextSelectedColor"] = EmulatorPage.ListSelectedTextColor;

Thank you to both of you!

like image 30
dingdangdowney Avatar answered Sep 22 '22 02:09

dingdangdowney