SFListview DataTemplate depending on Layout

Hello,

In my SFListview I want to have 2 different DataTemplate, one when the Layout is GridLayout and the other when the Layout is LinearLayout.

What is the best way to do this? do you have any example?

Thanks!

1 Reply 1 reply marked as answer

SS SaiGanesh Sakthivel Syncfusion Team April 23, 2021 09:57 AM UTC

Hi Genoveva,  
  
Thank you for using syncfusion support.  
  
#Regarding How to change the DataTemplate depending on Layout.  
We have checked your reported query from our end. You can achieve your requirements with the help of DataTemplateSelector. Inside the OnSelectTemplate override method, you can return the DataTemplate according to the SfListView ‘s layout from the container.  
  
Code snippet  
class MyDataTemplateSelector : Xamarin.Forms.DataTemplateSelector  
    {  
        public DataTemplate LinearLayoutDataTemplate { get; set; }  
        public DataTemplate GridLayoutDataTemplate { get; set; }  
  
        public MyDataTemplateSelector()  
        {  
  
        }  
  
        protected override DataTemplate OnSelectTemplate(object item, BindableObject container)  
        {  
            var listView = container as SfListView;  
            var layoutManager = listView.LayoutManager;  
            return (layoutManager is LinearLayout) ? this.LinearLayoutDataTemplate : this.GridLayoutDataTemplate;  
        }  
    }  
  
We have attached the tested sample for your reference and you can download the same from the following location. 
  
Please let us know if you have any concern.  
  
Regards,
SaiGanesh Sakthivel


Marked as answer
Loader.
Up arrow icon