Skip to content

Instantly share code, notes, and snippets.

@LGM-AdrianHum
Created October 11, 2017 23:56
Show Gist options
  • Save LGM-AdrianHum/c8cb125bc493c1ccac99b4098c7eeb60 to your computer and use it in GitHub Desktop.
Save LGM-AdrianHum/c8cb125bc493c1ccac99b4098c7eeb60 to your computer and use it in GitHub Desktop.
Changing WPF Listbox SelectedItem text color and highlight/background Color
<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>
@yue-qing-feng
Copy link

Thank you!

@DennisKost
Copy link

really good

@Georgemichael961
Copy link

Thanks

@richardlaveau
Copy link

Thank you.

@Davidos533
Copy link

Very nice!

@BaturalpDuran
Copy link

Thanks a lot! Trying to find this for days!

@LGM-AdrianHum
Copy link
Author

LGM-AdrianHum commented Jan 4, 2023 via email

@Rogatien
Copy link

Rogatien commented Aug 2, 2023

Ver helpful. Thank you.

@iSakha
Copy link

iSakha commented Jan 2, 2024

Awesome!Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment