Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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>
@ZyuDev

This comment has been minimized.

Copy link

@ZyuDev ZyuDev commented Aug 17, 2018

Thank you! It helps me a lot.

@Matix-Media

This comment has been minimized.

Copy link

@Matix-Media Matix-Media commented Apr 29, 2020

Amazing dude! Help a lot!

@ehgus8313

This comment has been minimized.

Copy link

@ehgus8313 ehgus8313 commented Aug 4, 2020

Thank you! It helps me a lot.

@iand5739

This comment has been minimized.

Copy link

@iand5739 iand5739 commented May 12, 2021

Thank you! It helps me a lot.

@yue-qing-feng

This comment has been minimized.

Copy link

@yue-qing-feng yue-qing-feng commented Jun 4, 2021

Thank you!

@DennisKost

This comment has been minimized.

Copy link

@DennisKost DennisKost commented Jun 10, 2021

really good

@Georgemichael961

This comment has been minimized.

Copy link

@Georgemichael961 Georgemichael961 commented Oct 17, 2021

Thanks

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