OnDataRequested Issue

6 posts, 1 answers
  1. Dimitris
    Dimitris avatar
    7 posts
    Member since:
    Aug 2013

    Posted 27 Aug 2013 Link to this post

    I'm using the databound listbox to load some items and I'm also using the OnDemandAutomatic DataVirtualization Mode, but I can't get it work.

    In my project when the page loads I use a method to bind a List<Item> with the databound listbox, I have subscribed to them OnDataRequested and below is my code for it
    void resultsLbox2_DataRequested(object sender, EventArgs e)
    {
        if (actorPagesUsed < actorPages)
        {
            actorPagesUsed++;
            tmdbAPI.SearchPerson(searchBox2.Text, actorPagesUsed, null, result =>
                 {
                     if (result.Data.results.Count == 0)
                         resultsLbox2.DataVirtualizationMode = DataVirtualizationMode.None;
                     else
                     {
                         foreach (PersonResult pr in result.Data.results)
                         {
                             Actor act = new Actor();
                             if (!string.IsNullOrEmpty(pr.profile_path))
                             {
                                 act.ID = pr.id;
                                 act.Name = pr.name;
                                 act.Image = new BitmapImage(new Uri(API.TMDBPrefixW185 + pr.profile_path));
                                 actorsResults.Add(act);
                                  
     
                                    resultsLbox2.ItemsSource = null;
                                    resultsLbox2.ItemsSource = actorsResults;
     
                             }
                         }
                     }
                 });
        }
    }

    the SearchPerson is an async method and inside of it is the callback, as you can see I kind of filter the returned items,
    add them to the first List<item> I have binded, set the itemssource to null and then bind it again but it doesn't work?


    what am i doing wrong here?
  2. Answer
    Deyan
    Admin
    Deyan avatar
    2041 posts

    Posted 28 Aug 2013 Link to this post

    Hello Dimitris,

    Thanks for writing.

    The way you handle the DataRequested event is correct to the point where you reset the ItemsSource of the listbox. You should not do that.

    The DataRequested event assumes that you have already bound your listbox to a collection with items. It simply allows you to append more items to the same collection when you reach the end of the scrollable list so that you can continue scrolling and viewing the newer items. When you change the source in the DataRequested event you will return to the beginning of the scrollable list and you will not get the behavior you want.

    What you can do here is extract the source collection into a class field and simply add the new items to it when the DataRequested event fires. YOu should make sure that you are using an OBservableCollection so that the listbox gets notified about new items being added.

    Let me know if you have further questions.

    Regards,
    Deyan
    Telerik
    Have a suggestion or face a problem - you can use the Ideas & Feedback portal to submit ideas, feedback and vote for them.
  3. DevCraft banner
  4. bento
    bento avatar
    35 posts
    Member since:
    Aug 2013

    Posted 04 Sep 2013 Link to this post

    Sorry, new with OnDataRequested. Could you give very simple sample using pivot with 2 pivotitems ? Your example on downloaded file source confuse me to understand.

    Thanks.
  5. Deyan
    Admin
    Deyan avatar
    2041 posts

    Posted 04 Sep 2013 Link to this post

    Hi,

    Could you please provide some further details on the scenario? How is the Pivot with the two Pivot pages related to the DataRequested event of RadDataBoundListBox?

    Regards,
    Deyan
    Telerik
    Have a suggestion or face a problem - you can use the Ideas & Feedback portal to submit ideas, feedback and vote for them.
  6. bento
    bento avatar
    35 posts
    Member since:
    Aug 2013

    Posted 06 Sep 2013 Link to this post

    This sample code below after line, for AllLatest raddataboundlistbox works well on first pivot. But second pivot contain Popular raddataboundlistbox only listing 10 items with animation displayed without stop. I debug and set breakpoint inside OnLoadDataRequest, but on Popular never come into that routine... whats wrong ? 

    I also tried separate with this but same result...
    AllLatest.DataRequested += this.OnLoadDataRequestLatest;
    Popular.DataRequested += this.OnLoadDataRequestPopular;

    ===================================================================================

    public Page1()
    {
        InitializeComponent();
        AllLatest.DataRequested += this.OnLoadDataRequest;
        Popular.DataRequested += this.OnLoadDataRequest;
        sourceAllLatest = new ObservableCollection<Article>();
        sourcePopular = new ObservableCollection<Article>();
        this.busyIndicator.IsRunning = false;
    }

    private async void OnLoadDataRequest(object sender, EventArgs e)
     {
         if (this.requestIssued)
         {
             return;
         }
     
         RadDataBoundListBox item = (RadDataBoundListBox)sender;
         string str = (string)item.Name;
     
         try
         {
             this.requestIssued = true;
             this.busyIndicator.IsRunning = false;
     
             if (str == "AllLatest")
             {
                 List<Article> articleLatest = await GetDataFromAPI.GetStories("article/latest/?", limit, currentIndexLatest);
                 foreach (Article s in articleLatest)
                 {
                     sourceAllLatest.Add(s);
                 }
                 currentIndexLatest = (short)(currentIndexLatest + limit + 1);
                 MessageBox.Show(articleLatest.Count.ToString());
     
                 if (this.sourceAllLatest.Count == 60)
                 {
                     this.AllLatest.DataVirtualizationMode = Telerik.Windows.Controls.DataVirtualizationMode.None;
                 }
             }
             else if (str == "Popular")
             {
                 List<Article> articlePopular = await GetDataFromAPI.GetStories("article/popular/?", limit, currentIndexPopular);
                 foreach (Article p in articlePopular)
                 {
                     sourcePopular.Add(p);
                 }
                 currentIndexPopular = (short)(currentIndexPopular + limit + 1);
                 if (this.sourcePopular.Count == 40)
                 {
                     this.Popular.DataVirtualizationMode = Telerik.Windows.Controls.DataVirtualizationMode.None;
                 }
             }
             this.requestIssued = false;
             this.busyIndicator.IsRunning = false;
         }
         catch (Exception ex)
         {
             this.requestIssued = false;
             MessageBox.Show(ex.Message);
         }
     }

  7. Deyan
    Admin
    Deyan avatar
    2041 posts

    Posted 09 Sep 2013 Link to this post

    Hi Benyamin,

    The problem with this code hides in the requestIssued flag. When the DataRequested event is fired from the first DataBoundListBox instance, this flag is raised. Immediately after that the DataRequested event will be fired for the second DataBoundListBox instance and since the flag will already be true you will never fetch any data for the second listbox. You may consider introducing 2 separate flags for your listboxes or simply provide two different event handlers for the DataRequested events.

    I hope this helps.

    Regards,
    Deyan
    Telerik
    Have a suggestion or face a problem - you can use the Ideas & Feedback portal to submit ideas, feedback and vote for them.
Back to Top
DevCraft banner