Xamarin Forms DataTemplateSelector
In this post we are going to use a very simple example to understand completely about the use of DataTemplateSelectors
Example Model
For example, lets create a model related to Roles or Identities, based on this we're going to select a specific template to show
From here we can have a simple list initialization on our base class
So now we're going to link this property on the Main Page
Now you can see the issue of displaying a slightly different template for different types of classes.
Data Templates
This part will be fun at final product, on this case we're going to define just a simple template based on Collection View Content, so on ResourceDictionary of your Page, add a Data Template for each model type.
DataTemplateSelector
To select which template is going to be used we have to implement a DataTemplateSelector. In your app lets create a new class that inherits from DataTemplateSelector
Back on ResourceDictionary , add a simple resource detailing the UserTemplateSelector
CollectionView ItemTemplate
To get a collectionView or CarousellView to use the custom DataTemplateSelector, all we have to do now is assign it into the ItemTemplate Property
That's it, finally I'm going to leave here below the repo URL where you can find the complete example.
To enjoy more of this content please share it and throw a like.