Locating SfDataPager control in Behaviour if it is located in a DataTemplate

Hi,

I have an issue using the Behaviour for using the SfDataPager with a ListView. The problem comes from trying to access the DataPager using the bindable.FindByName("dataPager"); method as this always returns null as the DataPager is part of a DataTemplate. 

 My XAML code is as follows: XAML:


XAML:

<syncfusion:sflistview x:name="JobListView" autofitmode="Height" horizontaloptions="CenterAndExpand" headersize="256" itemtemplate="{StaticResource JobListItem}" selectionmode="None" itemtapped="JobListView_ItemTapped">

                        <syncfusion:sflistview.headertemplate>
                            <datatemplate x:name="JobListHeaderTemplate">
                                <grid x:name="JobHeaderGrid" backgroundcolor="Transparent" heightrequest="256">
...
                                </grid>
                            </datatemplate>
                        </syncfusion:sflistview.headertemplate>


                        <syncfusion:sflistview.footertemplate>
                            <datatemplate x:name="JobListFooterTemplate">
                                <grid x:name="JobFooterGrid" backgroundcolor="White" verticaloptions="FillAndExpand">
                                    <grid.rowdefinitions>
                                        <rowdefinition height="Auto">
                                        <rowdefinition height="*">
                                    </rowdefinition></rowdefinition></grid.rowdefinitions>
                                    <sfpager:sfdatapager x:name="dataPager" heightrequest="45" horizontaloptions="CenterAndExpand" useondemandpaging="True" displaymode="PreviousNextNumeric" numericbuttoncount="5" pagesize="10" buttonshape="Rectangle" appearancemanager="{StaticResource customAppearance}">
                                    <label .....="">
                                </label></sfpager:sfdatapager></grid>
                            </datatemplate>
                        </syncfusion:sflistview.footertemplate>
                    </syncfusion:sflistview>


The Behaviour is the same as one of the samples: The part that fails is this: 


protected override void OnAttachedTo(ContentPage bindable)
{
  listView = bindable.FindByName("listView");
  dataPager = bindable.FindByName("dataPager");  <-- This cannot be found
  viewModel = new RequisitionListViewModel(); 
  dataPager.OnDemandLoading += DataPager_OnDemandLoading;
  base.OnAttachedTo(bindable);
} 


 Are there any other ways of achieving the desired results? 

Thanks for any help. 

 Andrew


3 Replies

LN Lakshmi Natarajan Syncfusion Team September 10, 2021 07:58 AM UTC

Hi Andrew, 
 
Thank you for using the Syncfusion products. 
 
We have checked the reported query “Locating SfDataPager control in Behaviour if it is located in a DataTemplate” from our side. We would like to inform you that we could not directly access the UI elements in the code behind class. Also, you can get the DataPager control by using Behavior for its direct parent control.  
 
Please refer to our online documentation to get the DataTemplate elements in the code behind, 
 
Also, please refer to our documentations to use paging with SfListView, 
 
Please let us know if you need further assistance. 
 
Lakshmi Natarajan 
 



AE Andrew Elliott September 10, 2021 12:08 PM UTC

Hi,

Thanks for the reply. I have looked at the documentation referenced in the above answer but I am not able to see how that would help the issue outlined in the original question ie. How to get a reference to the dataPager control in the Behaviour.

It seems I will not be able to use the Behaviour approach to get the reference to the DataPager.


I have tried a different approach with a sample included. I am now finding another issue:


        private void dataPager_OnDemandLoading(object sender, Syncfusion.SfDataGrid.XForms.DataPager.OnDemandLoadingEventArgs e)
{
var source = App.Locator.Main.Items.AsEnumerable();
var dataPager = (sender as SfDataPager);
dataPager.LoadDynamicItems(e.StartIndex, source.Skip(e.StartIndex).Take(e.PageSize));
JobListView.ItemsSource = dataPager.PagedSource; <-- Throws source is not IEnumerable<>
dataPager.PagedSource.Refresh();
}

The highlighted line throws an error of source is not IEnumerable<> ​at runtime.


I have included the project.

Any ideas?

Thanks

Andrew



Attachment: DataPagerSample_f47e3d7f.zip



LN Lakshmi Natarajan Syncfusion Team September 13, 2021 01:26 PM UTC

Hi Andrew, 
 
Thank you for the update. 
 
We have checked the reported query “The highlighted line throws an error of source is not IEnumerable<> ​at runtime” from our side. We would like to let you know that we have used the SfListView.ItemsSource as IEnumerable in our source level. Also, the DataPager.PagedSource is the type of PagedCollectionView which is not supported to bind to the ItemsSource property. Hence, we suggest you to get  the items from the ViewModel collection and set it to the ListView.             
 
Please refer to the following code snippets to overcome the reported crash, 
private void dataPager_OnDemandLoading(object sender, Syncfusion.SfDataGrid.XForms.DataPager.OnDemandLoadingEventArgs e) 
{ 
    var source = App.Locator.Main.Items.AsEnumerable(); 
    var dataPager = (sender as SfDataPager); 
             
    var items = source.Skip(e.StartIndex).Take(e.PageSize); 
    dataPager.LoadDynamicItems(e.StartIndex, items); 
    JobListView.ItemsSource = items.AsEnumerable(); 
    (dataPager.PagedSource as PagedCollectionView).ResetCache(); 
} 
 
We have modified the sample and attached in the following link, 
 
You can also refer to our user guidance document regarding OnDemandLoading from the following link, 
 
Please let us know if you need further assistance. 
 
Regards, 
Lakshmi Natarajan 


Loader.
Up arrow icon