Skip to content

Commit 85867a1

Browse files
Merge pull request #1181 from telerik/new-kb-reusing-itemtemplate-listpicker-dotnet-maui-220d54809ce8405aa1111463bb34e7af
Added new kb article reusing-itemtemplate-listpicker-dotnet-maui
2 parents 22a6fee + 0bf55bf commit 85867a1

File tree

1 file changed

+140
-0
lines changed

1 file changed

+140
-0
lines changed
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
---
2+
title: Reusing ItemTemplate with ListPicker in .NET MAUI
3+
description: Learn how to reuse a single ItemTemplate across multiple ListPickers in .NET MAUI using a value converter for dynamic data binding.
4+
type: how-to
5+
page_title: Reusing ItemTemplate Across ListPickers in .NET MAUI
6+
meta_title: Reusing ItemTemplate Across ListPickers in .NET MAUI
7+
slug: reusing-itemtemplate-listpicker-dotnet-maui
8+
tags: listpicker, .net maui, itemtemplate, displaymemberpath, converter
9+
res_type: kb
10+
---
11+
12+
## Environment
13+
14+
| Version | Product | Author |
15+
| --- | --- | ---- |
16+
| 11.0.0 | Telerik UI for .NET MAUI ListPicker | [Dobrinka Yordanova](https://www.telerik.com/blogs/author/dobrinka-yordanova) |
17+
18+
## Description
19+
20+
I want to reuse a single ItemTemplate across multiple instances of the [ListPicker]({%slug listpicker-overview%}) in my .NET MAUI application. Each ListPicker uses the `DisplayMemberPath` property but displays different types of data. I want to avoid manually overriding the `ItemTemplate` for each `RadListPicker` and instead define an implicit `ItemTemplate` that uses a value converter to display the correct data based on the ListPicker's data source.
21+
22+
This knowledge base article also answers the following questions:
23+
- How to use a common `ItemTemplate` for multiple `RadListPickers`?
24+
- How to dynamically bind the displayed text using `DisplayMemberPath`?
25+
- How to define an implicit `ItemTemplate` in `RadListPicker`?
26+
27+
## Solution
28+
29+
To reuse a single `ItemTemplate` across multiple instances of the ListPicker, use a value converter to dynamically bind the displayed text based on the type of data in the `RadListPicker`. The steps below demonstrate the suggested approach:
30+
31+
32+
**1.** Define the `RadListPickers` and set their `ItemTemplate` property to a static resource.
33+
34+
```xaml
35+
<VerticalStackLayout>
36+
<telerik:RadListPicker x:Name="picker"
37+
DisplayMemberPath="Country"
38+
Placeholder="Picker 1"
39+
ItemTemplate="{StaticResource commontemplate}" />
40+
<telerik:RadListPicker x:Name="picker2"
41+
Placeholder="Picker 2"
42+
DisplayMemberPath="Name"
43+
ItemTemplate="{StaticResource commontemplate}" />
44+
</VerticalStackLayout>
45+
```
46+
47+
**2.** Create classes for the data types used by the `RadListPickers`.
48+
49+
```csharp
50+
class Person2
51+
{
52+
public string Name { get; set; }
53+
public string Country { get; set; }
54+
}
55+
56+
class Country2
57+
{
58+
public string Name { get; set; }
59+
}
60+
```
61+
62+
**3.** Populate the ListPickers with data in the page's constructor.
63+
64+
```csharp
65+
public partial class MainPage : ContentPage
66+
{
67+
public MainPage()
68+
{
69+
InitializeComponent();
70+
71+
var people = new List<Person2>
72+
{
73+
new Person2 { Name = "John", Country = "USA" },
74+
new Person2 { Name = "Mary", Country = "UK" },
75+
new Person2 { Name = "Jake", Country = "Australia" }
76+
};
77+
picker.ItemsSource = people;
78+
79+
var countries = new List<Country2>
80+
{
81+
new Country2 { Name = "Great Britain" },
82+
new Country2 { Name = "UK" },
83+
new Country2 { Name = "Spain" }
84+
};
85+
picker2.ItemsSource = countries;
86+
}
87+
}
88+
```
89+
90+
**4.** Create a value converter to dynamically bind the displayed text.
91+
92+
```csharp
93+
public class ObjectToValueConverter : IValueConverter
94+
{
95+
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
96+
{
97+
if (value is Person2 person)
98+
{
99+
return person.Country;
100+
}
101+
102+
if (value is Country2 country)
103+
{
104+
return country.Name;
105+
}
106+
107+
return value;
108+
}
109+
110+
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
111+
{
112+
throw new NotImplementedException();
113+
}
114+
}
115+
```
116+
117+
**5.** Define a single `DataTemplate` in XAML that uses the value converter.
118+
119+
```xaml
120+
<ContentPage.Resources>
121+
<ResourceDictionary>
122+
<local:ObjectToValueConverter x:Key="ObjectToValueConverter" />
123+
<DataTemplate x:Key="commontemplate">
124+
<Grid RowDefinitions="*"
125+
ColumnDefinitions="*"
126+
HorizontalOptions="Fill">
127+
<Label Text="{Binding ., Converter={StaticResource ObjectToValueConverter}}" />
128+
</Grid>
129+
</DataTemplate>
130+
</ResourceDictionary>
131+
</ContentPage.Resources>
132+
```
133+
134+
This approach allows you to reuse the same `ItemTemplate` across multiple ListPickers by dynamically binding the displayed text based on the data source.
135+
136+
## See Also
137+
138+
- [ListPicker Overview]({%slug listpicker-overview%})
139+
- [Data Binding in .NET MAUI](https://docs.microsoft.com/en-us/dotnet/maui/fundamentals/data-binding/)
140+
- [Using Value Converters in .NET MAUI](https://learn.microsoft.com/en-us/dotnet/maui/fundamentals/data-binding/converters)

0 commit comments

Comments
 (0)