How-to create a templateSelector programatically?

1 Answer 316 Views
ListView
Comercializadora Paxia
Top achievements
Rank 1
Iron
Comercializadora Paxia asked on 08 Jun 2023, 07:39 PM
Hello, I find myself in need of creating a RadListView that uses templateSelector depending on the data it presents, in addition to this, for the creation of the itemTemplate I need to do it dynamically since some will present 4 radioButton or sometimes n radioButton. The question is: can I just create the item template in code form or is it necessary to create the whole RadListView? Do you have any examples or where I can see documentation of generating these elements by code?
Greetings!!

1 Answer, 1 is accepted

Sort by
0
Lance | Senior Manager Technical Support
Telerik team
answered on 08 Jun 2023, 09:13 PM

Hi Comercializadora Paxia,

Yes, this is possible because RadListView has support for DataTemplateSelector through the ItemTemplateSelector property. Please see this article => .NET MAUI ListView Documentation - ItemTemplateSelector .

The first thing to do is to learn how to use a DataTemplateSelector. This is not a Telerik feature, but rather a framework thing. So, you will need to learn how to do it using Microsoft resources. Here's a good start => https://learn.microsoft.com/en-us/dotnet/maui/fundamentals/datatemplate#create-a-datatemplateselector 

To your question of programmatically setting a template selector, it is like any other property. Create an instance of the thing you want to use... then assign it to the property you want to use it for. For example, lets say your selector class is named "MyTemplateSelector", it could look like this

As to why you would want to choose between multiple selectors doesn't make sense, because you can just use one selector and return the appropriate DataTemplate from inside it (that's the whole point of a selector to begin with :)

For example, lets say you have zoo animal data that could be deserialized into one of 3 classes; Cat, Dog, or Monkey


Inside the selector, you just check the data type and return the appropriate DataTemplate

Now that you have your selector set up, you can use it with any UI control, not just RadListView. But here's how that looks for the RadListView's ItemTemplateSelector

finally, at runtime, we are getting the correct DataTemplate for the respective data item:

As you can see, at the end of the day you have 100% control over the logic to return the DataTemplate you want. You can check for the data type itself, or a property on the data. The DataTemplateSelector doesn't care what you do inside the OnSelectTemplate method... you just need to return a DataTemplate.

Create DataTemplate With Only C#

To your other question of how to create a DataTemplate using only C#, yes this is possible and documented in many places. This question is also unrelated to Telerik and doesn't have an answer in the Telerik UI for Maui forums.

Search for "C# DataTemplate .NET MAUI" and you'll find great results like this on StackOverflow => c# - How to create DataTemplate with code? [MAUI] - Stack Overflow.

Note: as you many noticed by now, I have gone ahead an used all C# DataTemplate in my examples above. I could have XAML-defined ones, too, the only difference is in how you reference it. For exmaple, if the DataTemplate is in the page's Resources section, then you use this.

Regards,
Lance | Manager Technical Support
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Tags
ListView
Asked by
Comercializadora Paxia
Top achievements
Rank 1
Iron
Answers by
Lance | Senior Manager Technical Support
Telerik team
Share this question
or