In Windows Presentation Foundation (WPF), a DataTemplate defines how data objects are displayed in the user interface. However, sometimes an application needs to display different types of data using different layouts within the same control.
For example, consider an employee management application that displays permanent and contract employees in a ListBox. Permanent employees receive a bonus, while contract employees do not. You may want to display the bonus information only for permanent employees and use a different layout for contract employees.
WPF provides the DataTemplateSelector class to handle these scenarios. By creating a custom selector and overriding its SelectTemplate() method, you can choose the appropriate DataTemplate based on an object's properties or type.
In this tutorial, you will learn how to implement a custom DataTemplateSelector using C# and XAML, define multiple employee templates, and apply the selector to a WPF ListBox using data binding.
Prerequisites:- Basic knowledge of C# and WPF.
- Visual Studio with the WPF development workload installed.
- Familiarity with XAML, data binding, and DataTemplate.
Getting Started
WPF DataTemplateSelector class allows you to change the WPF DataTemplate dynamically at runtime. for example, let’s say you have a WPF ListBox where you want to display a list of employees and the employee details are varies based on an employee type(Permanente and Contract).
For example, a bonus is applicable to permanent employees and is not applicable for contract employees. You have to display the bonus field for permanent employees and hide that field for contract employees in WPF ListBox Item while displaying the employee list in ListBox.
There are various ways available in WPF to do that, but let’s say you want to display the employee’s details using the WPF Listbox item template and WPF DataTemplate.
To do that you have to declare a two different WPF DataTemplate to display different types of employee information at run time when DataSource (Employee List) binds to the ListBox and the ListBox should use the appropriate WPF DataTemplate for the employee based on the employee data.
WPF DataTemplateSelector class helps in this scenario, it allows switching WPF DataTemplate at runtime. Here in the below code example declare two different DataTeplate for Permanent Employee and Contract Employee and use those DataTemplate in WPF ListBox for employees based their type with the help of DataTemplateSelector class.
Demonstrtion
- Open Visual Studio and create a new WPF project with help of Template Window.
- Add a new class into project and name it as EmpDataTemplateSelector.
- Inherit the DataTemplateSelector class and implement the functions.
- Your EmpDataTemplateSelector class should look like the below code.
</> C#
public class EmpDataTemplateSelector : DataTemplateSelector { public override DataTemplate SelectTemplate(object item, DependencyObject container) { FrameworkElement element = container as FrameworkElement; if (element != null && item != null && item is Employee) { Employee emp = item as Employee; if (emp.Type == "Permanent") return element.FindResource("PerDataTemplate") as DataTemplate; else return element.FindResource("CorDataTemplate") as DataTemplate; } return null; } } - Open the MainWindow.xml in design mode and Windows resource declare a resource for your template selector class like the below code.
</> XAML
<Window.Resources> <local:EmpDataTemplateSelector x:Key="empDataTemplateSelector"/> </Window.Resources> - Add the below WPF ListBox code to your MainWindow.
</> XAML
<ListBox ItemsSource="{Binding Source={StaticResource myTodoList}}" ItemTemplateSelector="{StaticResource empDataTemplateSelector}" HorizontalContentAlignment="Stretch"/> - Now open the App.xml declare two different WPF DataTemplate in application resources like below code given in the button of this blog.
</> XAML
<Application.Resources>
<DataTemplate x:Key="PerEmployee">
<Border Padding="5" Margin="2" BorderThickness="1" Height="150" CornerRadius="5" BorderBrush="Black">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="50"/>
<RowDefinition Height="50"/>
<RowDefinition Height="50"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<TextBlock>
<Run Text="Employe Name :"/>
<Run Text="Permanent Employe "/>
</TextBlock>
<TextBlock>
<Run Text="Post Name :"/>
<Run Text="Engineer"/>
</TextBlock>
<TextBlock>
<Run Text="Bonous :"/>
<Run Text="Rs. 2,00,000.00"/>
</TextBlock>
</Grid>
</Border>
</DataTemplate>
<DataTemplate x:Key="ConEmployee">
<Border Padding="5" Margin="2" BorderThickness="1" Height="150" CornerRadius="5" BorderBrush="Black">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="50"/>
<RowDefinition Height="50"/>
<RowDefinition Height="50"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<TextBlock>
<Run Text="Employe Name :"/>
<Run Text="Contract Employe "/>
</TextBlock>
<TextBlock>
<Run Text="Post Name :"/>
<Run Text="Engineer"/>
</TextBlock>
</Grid>
</Border>
</DataTemplate>
</Application.Resources>
Summary
In the above, we demonstrated how to use the WPF DataTemplateSelector in WPF to switch between WPF DataTemplate at run time, I hope you have enjoyed it a lot.
Thanks