Overriding Styles for ListView and GridView in XAML Applications

By Ken Getz


Whether or not you're aware of it, every control that you use in a XAML application is based on a control template that's well defined and available for you to study and modify. In its documentation, Microsoft has provided information about each of these templates. The biggest reason you might care about the control templates is if you want to override some of the behavior of one of the controls. For example, both the ListView and GridView controls behave poorly when you use the Light theme in a XAML app. In this case, the selected item in the control appears with black text on a dark purple background, making it nearly impossible to read the text on the selected item. You might find it useful to modify this behavior, and it's easy.

To get started, find the documentation that describes the control template for the control you'd like to modify. For the ListView and GridView controls, it's the ListViewItem control that you should modify, and you can find the documentation for this control here:

Given this information, you can copy/paste the styles for the four items that might need changing into your own App.xaml file (or into a resource library referenced by App.xaml). For example, you could use code like the following to change the look of the selected item:

<SolidColorBrush x:Key="ListViewItemSelectedBackgroundThemeBrush" Color="#00abdc">
<SolidColorBrush x:Key="ListViewItemPointerOverBackgroundThemeBrush" Color="#1100abdc">
<SolidColorBrush x:Key="ListViewItemSelectedPointerOverBackgroundThemeBrush" Color="#de00abdc">
<SolidColorBrush x:Key="ListViewItemSelectedPointerOverBorderThemeBrush" Color="#ab00abdc">

By overriding the behavior of these specific resources, you can easily change the look of the selected item in a ListView or GridView control.

Ken Getz
Ken Getz is a featured instructor for several of our Visual Studio courses. He is a Visual Basic and Visual C# expert and has been recognized multiple times as a Microsoft MVP. Ken is a seasoned instructor, successful consultant, and the author or co-author of several best-selling books. He is a frequent speaker at technical conferences like Tech-Ed, VSLive, and DevConnections and he has written for several of the industry's most-respected publications including Visual Studio Magazine, CoDe Magazine, and MSDN Magazine.

This blog entry was originally posted January 03, 2013 by Ken Getz