Change WPF DataTemplate for ListBox item if selected
Data Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.
Introduction
In WPF, you can change the DataTemplate of a ListBox item when it is selected by using a DataTemplateSelector, a Style with DataTrigger, or visual state triggers. The most common approach is creating a custom DataTemplateSelector that returns different templates based on the item's selection state. This allows unselected items to show a compact view and selected items to show an expanded view with additional details or controls.
Using DataTemplateSelector
Define two templates and a selector that picks between them:
The selector class:
Register the selector in XAML:
Using Style Triggers (Simpler Approach)
Use ContentTemplate triggers on ListBoxItem style:
This approach is simpler — no C# code needed. The ListBoxItem style swaps the ContentTemplate when IsSelected changes.
Using DataTrigger with ViewModel Property
Bind selection state to a ViewModel property:
This keeps a single template but toggles element visibility based on selection.
Animating the Transition
Common Pitfalls
- DataTemplateSelector not updating on selection change:
DataTemplateSelector.SelectTemplateis called when the item is first loaded, not when selection changes. Force a re-evaluation by settingItemTemplateSelectorto null and back, or use the Style trigger approach which responds to property changes automatically. - Forgetting to walk the visual tree in the selector: The
containerparameter inSelectTemplateis theContentPresenter, not theListBoxItem. You must walk up the visual tree withVisualTreeHelper.GetParentto find theListBoxItemand checkIsSelected. - Using
ItemTemplateandItemTemplateSelectortogether: Setting bothItemTemplateandItemTemplateSelectoron the sameListBoxcausesItemTemplateto take precedence, ignoring the selector. Use one or the other, not both. - Not binding
IsSelectedtwo-way on the container style: When using the ViewModelIsSelectedpattern, the binding must beMode=TwoWayso that clicking a ListBoxItem updates the ViewModel property. WithoutTwoWay, the ViewModel property never changes. - Performance with complex selected templates: If the selected template contains heavy controls (images, grids, data-bound lists), rapidly switching selection causes layout thrashing. Use virtualization (
VirtualizingStackPanel.IsVirtualizing="True") and keep selected templates lightweight.
Summary
- Use
Style.TriggersonListBoxItemto swapContentTemplatebased onIsSelected— simplest approach - Use
DataTemplateSelectorfor more complex logic (e.g., different templates based on data type and selection) - Use
Visibilitybinding withBoolToVisibilityConverterwhen only parts of the template should change - Bind
ListBoxItem.IsSelectedto a ViewModel property withMode=TwoWayfor MVVM-friendly selection tracking DataTemplateSelectoris not re-evaluated on selection change — use triggers for dynamic switching- Keep selected templates lightweight to avoid layout performance issues
Related reading
- Changing SqlConnection timeout
- Channel/BlockingCollection alloc free alternatives?
- Check for column name in a SqlDataReader object
- Check if a value is in an array C
- Check if Key Exists in NameValueCollection
- Check if one IEnumerable contains all elements of another IEnumerable
- Checking for directory and file write permissions in .NET
- Checking for null before ToString

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.