Scroll down and Update Listview C#












1















I have a ListView in my UWP app.



This is my code:



<ListView x:Name="viewMedias" SelectionMode="None" VirtualizingStackPanel.VirtualizationMode="Recycling" ScrollViewer.HorizontalScrollMode="Disabled" ScrollViewer.VerticalScrollMode="Auto">
......
</ListView>


Now, how can I receive an event when this ListView scrolls to nearly end? I want to Update ItemsSource to add new Items to bottom of the ListView.










share|improve this question





























    1















    I have a ListView in my UWP app.



    This is my code:



    <ListView x:Name="viewMedias" SelectionMode="None" VirtualizingStackPanel.VirtualizationMode="Recycling" ScrollViewer.HorizontalScrollMode="Disabled" ScrollViewer.VerticalScrollMode="Auto">
    ......
    </ListView>


    Now, how can I receive an event when this ListView scrolls to nearly end? I want to Update ItemsSource to add new Items to bottom of the ListView.










    share|improve this question



























      1












      1








      1








      I have a ListView in my UWP app.



      This is my code:



      <ListView x:Name="viewMedias" SelectionMode="None" VirtualizingStackPanel.VirtualizationMode="Recycling" ScrollViewer.HorizontalScrollMode="Disabled" ScrollViewer.VerticalScrollMode="Auto">
      ......
      </ListView>


      Now, how can I receive an event when this ListView scrolls to nearly end? I want to Update ItemsSource to add new Items to bottom of the ListView.










      share|improve this question
















      I have a ListView in my UWP app.



      This is my code:



      <ListView x:Name="viewMedias" SelectionMode="None" VirtualizingStackPanel.VirtualizationMode="Recycling" ScrollViewer.HorizontalScrollMode="Disabled" ScrollViewer.VerticalScrollMode="Auto">
      ......
      </ListView>


      Now, how can I receive an event when this ListView scrolls to nearly end? I want to Update ItemsSource to add new Items to bottom of the ListView.







      c# xaml listview uwp






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 20 at 13:30









      walkerbox

      88211020




      88211020










      asked Jan 20 at 12:01









      sasansasan

      83




      83
























          1 Answer
          1






          active

          oldest

          votes


















          1














          Welcome to StackOverflow!
          What you want to achieve is called Incremental Loading. You don't have to implement it by your self - just check existing solutions.
          Windows Community Toolkit has IncrementalLoadingCollection helpers that can help you. You can check demo in their sample app: Windows Community Toolkit Sample App in Windows Store (go to helpers > Incremental Loading Collection). Here is some sample code from this app:



          // Defines a data source whose data can be loaded incrementally.
          using Microsoft.Toolkit.Uwp;

          public class Person
          {
          public string Name { get; set; }
          }

          public class PeopleSource : IIncrementalSource<Person>
          {
          private readonly List<Person> _people;

          public PeopleSource()
          {
          // Creates an example collection.
          _people = new List<Person>();

          for (int i = 1; i <= 200; i++)
          {
          var p = new Person { Name = "Person " + i };
          _people.Add(p);
          }
          }

          public async Task<IEnumerable<Person>> GetPagedItemsAsync(int pageIndex, int pageSize)
          {
          // Gets items from the collection according to pageIndex and pageSize parameters.
          var result = (from p in _people
          select p).Skip(pageIndex * pageSize).Take(pageSize);

          // Simulates a longer request...
          await Task.Delay(1000);

          return result;
          }
          }

          // IncrementalLoadingCollection can be bound to a GridView or a ListView. In this case it is a ListView called PeopleListView.

          var collection = new IncrementalLoadingCollection<PeopleSource, Person>();

          PeopleListView.ItemsSource = collection;

          // Binds the collection to the page DataContext in order to use its IsLoading and HasMoreItems properties.
          DataContext = collection;

          // XAML UI Element
          <TextBlock Text="{Binding IsLoading, Converter={StaticResource StringFormatConverter}, ConverterParameter='Is Loading: {0}'}" />
          <TextBlock Text="{Binding HasMoreItems, Converter={StaticResource StringFormatConverter}, ConverterParameter='Has More Items: {0}'}" />





          share|improve this answer
























          • thank you. I get items of the server. and each request get me 20 items. so how i can when scroll go to end, send an other request and add new items to the _people

            – sasan
            Jan 20 at 13:28











          • IIncrementalSource has method called GetPagedItemsAsync. This is the place where you should request for more items. You have to create your own Source adapted for your case.

            – walkerbox
            Jan 20 at 13:52











          • Also please note, PeopleSource instance is the ItemsSource here. _people variable is just a helper variable used to simulate real source of data (like server).

            – walkerbox
            Jan 20 at 13:54











          • Yes I know, But I don't want to get all the requests first. I need to have an event that when ended scroll, I will send a request to the server at that time. and add to _people Your code is only for the UX I need an event to send a request to the server and get new 20 items other

            – sasan
            Jan 20 at 14:11











          • GetPagedItemsAsync is your 'event' here. If you set ItemsSource to your custom source (eg. PeopleSource), GetPagedItemsAsync will be called when ListView is scrolled to bottom. What is returned from GetPagedItemsAsync is automatically added to bottom of your ListView.

            – walkerbox
            Jan 20 at 14:39











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54276211%2fscroll-down-and-update-listview-c-sharp%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          Welcome to StackOverflow!
          What you want to achieve is called Incremental Loading. You don't have to implement it by your self - just check existing solutions.
          Windows Community Toolkit has IncrementalLoadingCollection helpers that can help you. You can check demo in their sample app: Windows Community Toolkit Sample App in Windows Store (go to helpers > Incremental Loading Collection). Here is some sample code from this app:



          // Defines a data source whose data can be loaded incrementally.
          using Microsoft.Toolkit.Uwp;

          public class Person
          {
          public string Name { get; set; }
          }

          public class PeopleSource : IIncrementalSource<Person>
          {
          private readonly List<Person> _people;

          public PeopleSource()
          {
          // Creates an example collection.
          _people = new List<Person>();

          for (int i = 1; i <= 200; i++)
          {
          var p = new Person { Name = "Person " + i };
          _people.Add(p);
          }
          }

          public async Task<IEnumerable<Person>> GetPagedItemsAsync(int pageIndex, int pageSize)
          {
          // Gets items from the collection according to pageIndex and pageSize parameters.
          var result = (from p in _people
          select p).Skip(pageIndex * pageSize).Take(pageSize);

          // Simulates a longer request...
          await Task.Delay(1000);

          return result;
          }
          }

          // IncrementalLoadingCollection can be bound to a GridView or a ListView. In this case it is a ListView called PeopleListView.

          var collection = new IncrementalLoadingCollection<PeopleSource, Person>();

          PeopleListView.ItemsSource = collection;

          // Binds the collection to the page DataContext in order to use its IsLoading and HasMoreItems properties.
          DataContext = collection;

          // XAML UI Element
          <TextBlock Text="{Binding IsLoading, Converter={StaticResource StringFormatConverter}, ConverterParameter='Is Loading: {0}'}" />
          <TextBlock Text="{Binding HasMoreItems, Converter={StaticResource StringFormatConverter}, ConverterParameter='Has More Items: {0}'}" />





          share|improve this answer
























          • thank you. I get items of the server. and each request get me 20 items. so how i can when scroll go to end, send an other request and add new items to the _people

            – sasan
            Jan 20 at 13:28











          • IIncrementalSource has method called GetPagedItemsAsync. This is the place where you should request for more items. You have to create your own Source adapted for your case.

            – walkerbox
            Jan 20 at 13:52











          • Also please note, PeopleSource instance is the ItemsSource here. _people variable is just a helper variable used to simulate real source of data (like server).

            – walkerbox
            Jan 20 at 13:54











          • Yes I know, But I don't want to get all the requests first. I need to have an event that when ended scroll, I will send a request to the server at that time. and add to _people Your code is only for the UX I need an event to send a request to the server and get new 20 items other

            – sasan
            Jan 20 at 14:11











          • GetPagedItemsAsync is your 'event' here. If you set ItemsSource to your custom source (eg. PeopleSource), GetPagedItemsAsync will be called when ListView is scrolled to bottom. What is returned from GetPagedItemsAsync is automatically added to bottom of your ListView.

            – walkerbox
            Jan 20 at 14:39
















          1














          Welcome to StackOverflow!
          What you want to achieve is called Incremental Loading. You don't have to implement it by your self - just check existing solutions.
          Windows Community Toolkit has IncrementalLoadingCollection helpers that can help you. You can check demo in their sample app: Windows Community Toolkit Sample App in Windows Store (go to helpers > Incremental Loading Collection). Here is some sample code from this app:



          // Defines a data source whose data can be loaded incrementally.
          using Microsoft.Toolkit.Uwp;

          public class Person
          {
          public string Name { get; set; }
          }

          public class PeopleSource : IIncrementalSource<Person>
          {
          private readonly List<Person> _people;

          public PeopleSource()
          {
          // Creates an example collection.
          _people = new List<Person>();

          for (int i = 1; i <= 200; i++)
          {
          var p = new Person { Name = "Person " + i };
          _people.Add(p);
          }
          }

          public async Task<IEnumerable<Person>> GetPagedItemsAsync(int pageIndex, int pageSize)
          {
          // Gets items from the collection according to pageIndex and pageSize parameters.
          var result = (from p in _people
          select p).Skip(pageIndex * pageSize).Take(pageSize);

          // Simulates a longer request...
          await Task.Delay(1000);

          return result;
          }
          }

          // IncrementalLoadingCollection can be bound to a GridView or a ListView. In this case it is a ListView called PeopleListView.

          var collection = new IncrementalLoadingCollection<PeopleSource, Person>();

          PeopleListView.ItemsSource = collection;

          // Binds the collection to the page DataContext in order to use its IsLoading and HasMoreItems properties.
          DataContext = collection;

          // XAML UI Element
          <TextBlock Text="{Binding IsLoading, Converter={StaticResource StringFormatConverter}, ConverterParameter='Is Loading: {0}'}" />
          <TextBlock Text="{Binding HasMoreItems, Converter={StaticResource StringFormatConverter}, ConverterParameter='Has More Items: {0}'}" />





          share|improve this answer
























          • thank you. I get items of the server. and each request get me 20 items. so how i can when scroll go to end, send an other request and add new items to the _people

            – sasan
            Jan 20 at 13:28











          • IIncrementalSource has method called GetPagedItemsAsync. This is the place where you should request for more items. You have to create your own Source adapted for your case.

            – walkerbox
            Jan 20 at 13:52











          • Also please note, PeopleSource instance is the ItemsSource here. _people variable is just a helper variable used to simulate real source of data (like server).

            – walkerbox
            Jan 20 at 13:54











          • Yes I know, But I don't want to get all the requests first. I need to have an event that when ended scroll, I will send a request to the server at that time. and add to _people Your code is only for the UX I need an event to send a request to the server and get new 20 items other

            – sasan
            Jan 20 at 14:11











          • GetPagedItemsAsync is your 'event' here. If you set ItemsSource to your custom source (eg. PeopleSource), GetPagedItemsAsync will be called when ListView is scrolled to bottom. What is returned from GetPagedItemsAsync is automatically added to bottom of your ListView.

            – walkerbox
            Jan 20 at 14:39














          1












          1








          1







          Welcome to StackOverflow!
          What you want to achieve is called Incremental Loading. You don't have to implement it by your self - just check existing solutions.
          Windows Community Toolkit has IncrementalLoadingCollection helpers that can help you. You can check demo in their sample app: Windows Community Toolkit Sample App in Windows Store (go to helpers > Incremental Loading Collection). Here is some sample code from this app:



          // Defines a data source whose data can be loaded incrementally.
          using Microsoft.Toolkit.Uwp;

          public class Person
          {
          public string Name { get; set; }
          }

          public class PeopleSource : IIncrementalSource<Person>
          {
          private readonly List<Person> _people;

          public PeopleSource()
          {
          // Creates an example collection.
          _people = new List<Person>();

          for (int i = 1; i <= 200; i++)
          {
          var p = new Person { Name = "Person " + i };
          _people.Add(p);
          }
          }

          public async Task<IEnumerable<Person>> GetPagedItemsAsync(int pageIndex, int pageSize)
          {
          // Gets items from the collection according to pageIndex and pageSize parameters.
          var result = (from p in _people
          select p).Skip(pageIndex * pageSize).Take(pageSize);

          // Simulates a longer request...
          await Task.Delay(1000);

          return result;
          }
          }

          // IncrementalLoadingCollection can be bound to a GridView or a ListView. In this case it is a ListView called PeopleListView.

          var collection = new IncrementalLoadingCollection<PeopleSource, Person>();

          PeopleListView.ItemsSource = collection;

          // Binds the collection to the page DataContext in order to use its IsLoading and HasMoreItems properties.
          DataContext = collection;

          // XAML UI Element
          <TextBlock Text="{Binding IsLoading, Converter={StaticResource StringFormatConverter}, ConverterParameter='Is Loading: {0}'}" />
          <TextBlock Text="{Binding HasMoreItems, Converter={StaticResource StringFormatConverter}, ConverterParameter='Has More Items: {0}'}" />





          share|improve this answer













          Welcome to StackOverflow!
          What you want to achieve is called Incremental Loading. You don't have to implement it by your self - just check existing solutions.
          Windows Community Toolkit has IncrementalLoadingCollection helpers that can help you. You can check demo in their sample app: Windows Community Toolkit Sample App in Windows Store (go to helpers > Incremental Loading Collection). Here is some sample code from this app:



          // Defines a data source whose data can be loaded incrementally.
          using Microsoft.Toolkit.Uwp;

          public class Person
          {
          public string Name { get; set; }
          }

          public class PeopleSource : IIncrementalSource<Person>
          {
          private readonly List<Person> _people;

          public PeopleSource()
          {
          // Creates an example collection.
          _people = new List<Person>();

          for (int i = 1; i <= 200; i++)
          {
          var p = new Person { Name = "Person " + i };
          _people.Add(p);
          }
          }

          public async Task<IEnumerable<Person>> GetPagedItemsAsync(int pageIndex, int pageSize)
          {
          // Gets items from the collection according to pageIndex and pageSize parameters.
          var result = (from p in _people
          select p).Skip(pageIndex * pageSize).Take(pageSize);

          // Simulates a longer request...
          await Task.Delay(1000);

          return result;
          }
          }

          // IncrementalLoadingCollection can be bound to a GridView or a ListView. In this case it is a ListView called PeopleListView.

          var collection = new IncrementalLoadingCollection<PeopleSource, Person>();

          PeopleListView.ItemsSource = collection;

          // Binds the collection to the page DataContext in order to use its IsLoading and HasMoreItems properties.
          DataContext = collection;

          // XAML UI Element
          <TextBlock Text="{Binding IsLoading, Converter={StaticResource StringFormatConverter}, ConverterParameter='Is Loading: {0}'}" />
          <TextBlock Text="{Binding HasMoreItems, Converter={StaticResource StringFormatConverter}, ConverterParameter='Has More Items: {0}'}" />






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 20 at 12:19









          walkerboxwalkerbox

          88211020




          88211020













          • thank you. I get items of the server. and each request get me 20 items. so how i can when scroll go to end, send an other request and add new items to the _people

            – sasan
            Jan 20 at 13:28











          • IIncrementalSource has method called GetPagedItemsAsync. This is the place where you should request for more items. You have to create your own Source adapted for your case.

            – walkerbox
            Jan 20 at 13:52











          • Also please note, PeopleSource instance is the ItemsSource here. _people variable is just a helper variable used to simulate real source of data (like server).

            – walkerbox
            Jan 20 at 13:54











          • Yes I know, But I don't want to get all the requests first. I need to have an event that when ended scroll, I will send a request to the server at that time. and add to _people Your code is only for the UX I need an event to send a request to the server and get new 20 items other

            – sasan
            Jan 20 at 14:11











          • GetPagedItemsAsync is your 'event' here. If you set ItemsSource to your custom source (eg. PeopleSource), GetPagedItemsAsync will be called when ListView is scrolled to bottom. What is returned from GetPagedItemsAsync is automatically added to bottom of your ListView.

            – walkerbox
            Jan 20 at 14:39



















          • thank you. I get items of the server. and each request get me 20 items. so how i can when scroll go to end, send an other request and add new items to the _people

            – sasan
            Jan 20 at 13:28











          • IIncrementalSource has method called GetPagedItemsAsync. This is the place where you should request for more items. You have to create your own Source adapted for your case.

            – walkerbox
            Jan 20 at 13:52











          • Also please note, PeopleSource instance is the ItemsSource here. _people variable is just a helper variable used to simulate real source of data (like server).

            – walkerbox
            Jan 20 at 13:54











          • Yes I know, But I don't want to get all the requests first. I need to have an event that when ended scroll, I will send a request to the server at that time. and add to _people Your code is only for the UX I need an event to send a request to the server and get new 20 items other

            – sasan
            Jan 20 at 14:11











          • GetPagedItemsAsync is your 'event' here. If you set ItemsSource to your custom source (eg. PeopleSource), GetPagedItemsAsync will be called when ListView is scrolled to bottom. What is returned from GetPagedItemsAsync is automatically added to bottom of your ListView.

            – walkerbox
            Jan 20 at 14:39

















          thank you. I get items of the server. and each request get me 20 items. so how i can when scroll go to end, send an other request and add new items to the _people

          – sasan
          Jan 20 at 13:28





          thank you. I get items of the server. and each request get me 20 items. so how i can when scroll go to end, send an other request and add new items to the _people

          – sasan
          Jan 20 at 13:28













          IIncrementalSource has method called GetPagedItemsAsync. This is the place where you should request for more items. You have to create your own Source adapted for your case.

          – walkerbox
          Jan 20 at 13:52





          IIncrementalSource has method called GetPagedItemsAsync. This is the place where you should request for more items. You have to create your own Source adapted for your case.

          – walkerbox
          Jan 20 at 13:52













          Also please note, PeopleSource instance is the ItemsSource here. _people variable is just a helper variable used to simulate real source of data (like server).

          – walkerbox
          Jan 20 at 13:54





          Also please note, PeopleSource instance is the ItemsSource here. _people variable is just a helper variable used to simulate real source of data (like server).

          – walkerbox
          Jan 20 at 13:54













          Yes I know, But I don't want to get all the requests first. I need to have an event that when ended scroll, I will send a request to the server at that time. and add to _people Your code is only for the UX I need an event to send a request to the server and get new 20 items other

          – sasan
          Jan 20 at 14:11





          Yes I know, But I don't want to get all the requests first. I need to have an event that when ended scroll, I will send a request to the server at that time. and add to _people Your code is only for the UX I need an event to send a request to the server and get new 20 items other

          – sasan
          Jan 20 at 14:11













          GetPagedItemsAsync is your 'event' here. If you set ItemsSource to your custom source (eg. PeopleSource), GetPagedItemsAsync will be called when ListView is scrolled to bottom. What is returned from GetPagedItemsAsync is automatically added to bottom of your ListView.

          – walkerbox
          Jan 20 at 14:39





          GetPagedItemsAsync is your 'event' here. If you set ItemsSource to your custom source (eg. PeopleSource), GetPagedItemsAsync will be called when ListView is scrolled to bottom. What is returned from GetPagedItemsAsync is automatically added to bottom of your ListView.

          – walkerbox
          Jan 20 at 14:39




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54276211%2fscroll-down-and-update-listview-c-sharp%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Liquibase includeAll doesn't find base path

          How to use setInterval in EJS file?

          Petrus Granier-Deferre