How to render different types of ViewCell in the same ListView during runtime?

Technology CommunityCategory: XamarinHow to render different types of ViewCell in the same ListView during runtime?
VietMX Staff asked 3 years ago

Xamarin.Forms implements DataTemplateSelector (introduced in Xamarin.Forms V 2.1), which provides a way to customize the presentation of a Cell at Runtime. For example, if we want to create a ListView which displays the chat messages, then we display incoming message to the Left and outgoing messages to the Right along with different colors. We can achieve this using the DataTemplateSelector.

We have to override the DataTemplateSelector class and design different DataTemplates for Incoming and Outgoing messages and then, at runtime, we can decide which one to select for the ListView item based on the bound data.