WPF 4: Style ListBox Items using ItemContainer Style

Recently I was asked a question about the default styles for WPF ListBoxItems and how to change the Style for the a Selected ListBox Item. This article has the solution to both the queries.

When data is bound to a WPF List Box, the List Items gets generated automatically and it becomes a bit clumsy to apply any style on the individual items in the ListBox. In such scenarios, we can implement the ItemContainerStyle property. Once this property is set, the style applied is set for every item in the ListBox when it is created.

In the code shown below, I have explained how an ItemContainerStyle can be applied. I have created a WPF application and added the following class in it:

image

The XAML code shown below defines the ItemContainerStyle for the ListBox:

WPF ListBox StyleWPF ListBox Style

Now run the application and the following result will be displayed:


WPF ListBox ItemContainer
and move the mouse cursor over each ListBoxItem, the following result will be displayed

WPF ListBox ItemContainer

The ItemContainerStyle provides a mechanism for applying styles formatting on individual WPF ListBox items.






About The Author

Mahesh Sabnis is a Microsoft MVP having over 18 years of experience in IT education and development. He is a Microsoft Certified Trainer (MCT) since 2005 and has conducted various Corporate Training programs for .NET Technologies (all versions). He also blogs regularly at DotNetCurry.com. Follow him on twitter @maheshdotnet

1 comment:

Anonymous said...

The background property on mouse over does not work on Windows 8 (the background is still white, the default color of my theme)