1

I am creating a Windows phone 8.1 application

I have a generated a list of textboxes RepTb in a listview. But I cannot figure out a way to get the text in these textboxes.

<Grid>
    <Grid.Resources>
        <DataTemplate x:Name="items">
            <TextBox Name="RepTb" Header="{Binding}" 
                     Width="100" HorizontalAlignment="Left"
                     LostFocus="RepTb_LostFocus"/>
        </DataTemplate>
    </Grid.Resources>

    <ListView x:Uid="RepListView"
              ItemsSource="{Binding}"
              ItemTemplate="{StaticResource items}"
              Name="RepList"/>
</Grid>

Code used to create textboxes:

 List<string> setlist = new List<string>();
 int set = 10;
 for (int i = 1; i <= set; i++ )
      setlist.Add("Reps in Set " + i);
 RepList.DataContext = setlist;

Can anyone tell me how to iterate through RepList and get the content off the textboxes?

tirtha2shredder
  • 105
  • 1
  • 7

1 Answers1

2

You can use a TwoWay data binding to get value of your textbox.

First you need to create a class which will contain your data.

public class RepItem
{
    public string Header {get; set;}
    public string Value {get;set;}
}

Then inject a List instead of a List

List<RepItem> setlist = new List<RepItem>();
 int set = 10;
 for (int i = 1; i <= set; i++ )
      setlist.Add(new RepItem() {Header = "Reps in Set " + i});
 RepList.DataContext = setlist;

Finally, bind the TextBox Text property to Value property of your RepItem object :

<DataTemplate x:Name="items">
        <TextBox Name="RepTb" 
                 Header="{Binding Header}" 
                 Text="{Binding Value, Mode=TwoWay}"
                 Width="100" 
                 HorizontalAlignment="Left"
                 LostFocus="RepTb_LostFocus"/>
 </DataTemplate>

You can use Text="{Binding Value, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" if you want that the Value property is updated each time you type a character, otherwise it will be updated when hte focus on the TextBox is lost.

You will be able to iterate through your List and get all the values

((List<RepItem>)RepList.DataContext).Select(repItem => repItem.Value);
Lucas Loegel
  • 101
  • 3