Forum Xamarin Xamarin.Forms

[DataTemplateSelector] How to modify an item's DataTemplate after this last one has been draw ?


Hi guys,

I have a silly question and I think that I already know the answer, but still want to post it to be 100% sure.

I developed a paginable listview that will load items 20 by 20. Here is how it's working:

  • load the first 20 items
  • user is scrolling down and meeting the 20th item
  • load the next 20 items

As you can see, my listing is limited to 20 items at any time and when the user would scroll down in a fast way, I would set some empty viewmodels to prevent the UI to be blocked by the awaitable API calls and so to allow the user to keep scrolling further. Once the user would stop scrolling, the last API call (previous ones would be cancelled of course) returning my data would fill these empty viewmodels and the wanted data would then be displayed.

My issue is the next one: I would like to implement multiple display depending on my items' properties values. I read the documentation about the DataTemplateSelector and implemented it into my project. It's working fine, but since I set some empty viewmodels when the user would scroll in a fast way, the received item into the OnSelectedTemplate method is not containing the needed data to know which DataTemplete to select.

Therefore I had to set a default UI for this specific context. My question is now : is there a way to re-set a DataTemplate after the OnSelectedTemplate method already has been executed ?

Hoping my explanations were clear enough, I thank you in advance for your responses and your time !
Best regards,

Best Answer


  • cahyoagungcahyoagung Member ✭✭

    Here some of my templete selector

    public class TemplateSelector : DataTemplateSelector {
        public DataTemplate ValidTemplate { get; set; }
        public DataTemplate ReloadTemplate { get; set; }
        public DataTemplate EmptyTemplate { get; set; }
        protected override DataTemplate OnSelectTemplate(object item, BindableObject container) {
            var obj = item as Model.WorkOrder;
            switch (obj.ItemType) {
                case Model.ItemType.Reload:
                    return ReloadTemplate;
                case Model.ItemType.End:
                    return EmptyTemplate;
                    return ValidTemplate;

    as we now that item stand for BindingContext for each Cell, update this and when OnSelectTemplate fired again choose the right template

    Note: obj.ItemType is an enum

  • MT_WSMMT_WSM FRMember ✭✭

    Hi @LandLu ,

    Thank you for your response, I will try to modify my paginable list by following your advices.
    Again thank you for everything !

    @cahyoagung: thank you to you too, but I was not asking about how to implement the DataTemplateSelector system.
    I already succeed to do so. ;)

    Best regards,

Sign In or Register to comment.