• Javascript
  • Python
  • Go

Custom Styling Selected Item in a WPF XAML ListBox

The WPF XAML ListBox is a powerful tool for displaying a list of items in a user-friendly and visually appealing manner. However, sometimes ...

The WPF XAML ListBox is a powerful tool for displaying a list of items in a user-friendly and visually appealing manner. However, sometimes it's necessary to add some custom styling to make selected items stand out. In this article, we will explore how to achieve this using HTML tags formatting.

First, let's start by creating a simple ListBox in our XAML code:

<ListBox>

<ListBoxItem Content="Item 1"/>

<ListBoxItem Content="Item 2"/>

<ListBoxItem Content="Item 3"/>

<ListBoxItem Content="Item 4"/>

<ListBoxItem Content="Item 5"/>

</ListBox>

This will give us a basic ListBox with five items. Now, let's add some custom styling to make the selected item stand out.

To do this, we will use the HTML <b> tag to make the selected item bold and the <i> tag to make it italic. We will also use the <font> tag to change the color of the selected item. Here's how our new XAML code will look like:

<ListBox>

<ListBoxItem Content="Item 1"/>

<ListBoxItem Content="Item 2"/>

<ListBoxItem Content="Item 3"/>

<ListBoxItem Content="Item 4"/>

<ListBoxItem Content="Item 5"/>

<ListBox.ItemContainerStyle>

<Style TargetType="ListBoxItem">

<Setter Property="Template">

<Setter.Value>

<ControlTemplate TargetType="ListBoxItem">

<ContentPresenter/>

</ControlTemplate>

</Setter.Value>

</Setter>

<Style.Triggers>

<Trigger Property="IsSelected" Value="True">

<Setter Property="ContentTemplate">

<Setter.Value>

<DataTemplate>

<TextBlock>

<Run Text="<b><i><font color='red'>"/>

<Run Text="{Binding}"/>

<Run Text="</font></i></b>"/>

</TextBlock>

</DataTemplate>

</Setter.Value>

</Setter>

</Trigger>

</Style.Triggers>

</Style>

</ListBox.ItemContainerStyle>

</ListBox>

Let's break down this code and understand what's happening. First, we have added a new <Style> tag inside the <ListBox> tag. This style will target all the ListBoxItems in our ListBox. We have set the <Setter Property="Template"> to a <ControlTemplate> with a <ContentPresenter> inside it. This will ensure that the default template for ListBoxItems is used.

Next, we have added a <Trigger> to our style. This trigger will be activated when the IsSelected property of a ListBoxItem is set to true. Inside the trigger, we have set the <Setter Property="ContentTemplate"> to a <DataTemplate> with a <TextBlock> inside it. This <TextBlock> will contain our custom HTML tags formatting.

Inside the <TextBlock>, we have used the <Run> tag to display the content of the ListBoxItem. But before and after the content, we have added the <b>, <i>, and <font> tags to make it bold, italic, and red in color, respectively.

Now, when we run our code and select an item in the ListBox, we can see that the selected item is displayed in a bold, italic, and red font. This custom

Related Articles

WPF ListBoxItem Selection Problem

WPF (Windows Presentation Foundation) is a popular framework for building modern desktop applications on the Windows platform. With its rich...

WPF Style Trigger

WPF Style Trigger: Enhancing Your User Interface Design In the world of software development, user interface design plays a crucial role in ...