locked
ListBox loop through all items RRS feed

  • Question

  • Hi! I have an app that lists many rows with Columns like Article and Qty. Each row(item) has aItemTemplate/DataTemplate with Article as a TextBlock and Qty as a TextBox. I then want the user to be able to change Qty for x number of rows and then press Save. When Save is pressed i would like to loop through all Qty Textboxes and update the underlying datasource. So my question is how can i loop through all items? Kindly, Mattias
    Wednesday, December 1, 2010 1:07 PM

Answers

  • If your ListBox is using DataBinding to populate its items, then any changes you make in the Qty TextBox should get reflected back to the Source objects. Then, you can go through and save the changes from the underlying collection of objects back to your datasource.
    Wednesday, December 1, 2010 4:04 PM

All replies

  • If your ListBox is using DataBinding to populate its items, then any changes you make in the Qty TextBox should get reflected back to the Source objects. Then, you can go through and save the changes from the underlying collection of objects back to your datasource.
    Wednesday, December 1, 2010 4:04 PM
  • To look at controls you just look at the Children collection on the parent control. So, If you had a grid named MyGird. To loop through all the text boxes you have in the grid just do this:


    foreach (UIElement t in MyGrid.Children)
    {
            TextBox te = t as TextBox;

    if(TextBox != null)
    {
    //do stuff to text box here
    }
    }
    Wednesday, December 1, 2010 4:10 PM
  • Perfect. I only had to set Binding Mode to ToWay on the Textbox to make it work. Thank you! /Mattias
    Thursday, December 2, 2010 9:20 AM
  • Perfect. I only had to set Binding Mode to ToWay on the Textbox to make it work.Thank you!/Mattias

    That's interesting, because I believe TextBox sets it binding mode to TwoWay by default. Glad it worked!
    Thursday, December 2, 2010 1:42 PM