locked
Add items to listbox on tap of textblock

    Question

  • I am developing an app in which I need to load more items in listbox and newly downloaded items to be added to existing listbox. Is it possible? How?
    Tuesday, February 24, 2015 10:44 AM

Answers

  • Well, maybe you should tell us how you populate the ListBox then? If you have set the ItemsSource property of the ListBox to some collection you should add the objects (the strings or whatever you are using) to this collection instead of adding them directly to the Items property, e.g:

    var collection = list.ItemsSource as List<string>; //of course you need to cast to the right type here
    if(collection != null)
     collection.Add(txt.Text);

    But please don't expect anyone to know what is going on in your code without posting any details.


    Please also remember to mark helpful posts as answer and/or helpful.

    • Marked as answer by RohitrkKUmar Tuesday, March 03, 2015 11:46 AM
    Tuesday, February 24, 2015 1:11 PM

All replies

  • A TextBlock has a Tapped event which you could handle. The following sample code should give you the idea:

    <TextBlock Text="some text..." Tapped="Text_Tapped" />
    
    <ListBox x:Name="list" />
    


    void Text_Tapped(object sender, TappedRoutedEventArgs e)
    {
        TextBlock txt = sender as TextBlock;
        if(txt != null)
        {
     list.Items.Add(txt.Text);
        }
    }

    Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question.
    • Proposed as answer by Dave SmitsMVP Tuesday, February 24, 2015 11:46 AM
    Tuesday, February 24, 2015 11:26 AM
  • I tried this approach but it shows Catastrophic failure (Exception from HRESULT: 0x8000FFFF (E_UNEXPECTED)) this error while adding items to listbox.
    Tuesday, February 24, 2015 1:07 PM
  • Well, maybe you should tell us how you populate the ListBox then? If you have set the ItemsSource property of the ListBox to some collection you should add the objects (the strings or whatever you are using) to this collection instead of adding them directly to the Items property, e.g:

    var collection = list.ItemsSource as List<string>; //of course you need to cast to the right type here
    if(collection != null)
     collection.Add(txt.Text);

    But please don't expect anyone to know what is going on in your code without posting any details.


    Please also remember to mark helpful posts as answer and/or helpful.

    • Marked as answer by RohitrkKUmar Tuesday, March 03, 2015 11:46 AM
    Tuesday, February 24, 2015 1:11 PM
  • It worked Thanks for this.
    Tuesday, March 03, 2015 11:46 AM