Reputation: 197
I'm trying to create table scheme in Windows phone 8.1 but I have problem with saving this. I created table in XAML: Here is code
<ItemsControl x:Name="br" ItemsSource="{Binding Data}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid x:Name="Ahoj" Margin="0,0,-20,-18">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="100"/>
</Grid.ColumnDefinitions>
<TextBox Grid.Column="0" Text="{Binding name}"></TextBox>
<TextBox Grid.Column="1" Text="{Binding s1}"></TextBox>
<TextBox Grid.Column="2" Text="{Binding s2}"></TextBox>
<TextBox Grid.Column="3" Text="{Binding s3}"></TextBox>
<TextBox Grid.Column="4" Text="{Binding s3}"></TextBox>
<TextBox Grid.Column="5" Text="{Binding name}"></TextBox>
<TextBox Grid.Column="6" Text="{Binding s1}"></TextBox>
<TextBox Grid.Column="7" Text="{Binding s2}"></TextBox>
<TextBox Grid.Column="8" Text="{Binding s3}"></TextBox>
<TextBox Grid.Column="9" Text="{Binding s3}"></TextBox>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
but i don't know how to read values from this dynamically created textBoxes. I need to get the value of all the textbox. I do not need to work with them separately. Thanks
EDIT
I try to use codes from answers and it work well, but only with first grid. I'm creating grid dynamically too. This grid has the same name, but diferent values from Binding. Code in answer work returning value only from first line textboxes...
Code - I'm adding items to list and after Itemsource is this list and I'm binding it to textboxes
var str = new StreamReader(contentStream);
while(str.EndOfStream !=true)
{
string line = str.ReadLine();
if (line == null)
break;
var spl = line.Split(';');
string prvni = spl[0].ToString();
if(spl[0]!="")
{
if (spl[0].Substring(0,3).Contains("-"))
{
obj.Add(new data(a+pocet.ToString(),spl[0].ToString(), spl[1].ToString(), spl[2].ToString(),"#FF00D1FF"));
}
else
obj.Add(new data(a+pocet.ToString(),spl[0].ToString(), spl[1].ToString(), spl[2].ToString(), "White"));
}
else
{
obj.Add(new data(a + pocet.ToString(), spl[0].ToString(), spl[1].ToString(), spl[2].ToString(), "White"));
}
pocet++;
}
br.ItemsSource = obj; // load list to binding
Class data
public class data
{
public string Index { get; set; }
public string s1 { get; set; }
public string s2 { get; set; }
public string s3 { get; set; }
public string color { get; set; }
public data() { }
public data(string index,string s1, string s2, string s3, string br)
{
this.Index = index;
this.s1 = s1;
this.s2 = s2;
this.s3 = s3;
this.color = br;
}
}
Upvotes: 0
Views: 239
Reputation: 726
I've also worked with dynamic content and I used to do this: ( Adapted to your requirements )
// Cycle through every control from Ahoj
foreach (Object controlObject in Ahoj.Children) {
if (controlObject is TextBox) {
TextBox
textBox = controlObject as TextBox;
// Do your stuff here...
}
}
Since you say that you're unable to access the Grid
directly ( if I understood correctly, you're adding the control by run-time code ) then you can do something like this:
try {
Grid
grid = FindName("Ahoj") as Grid;
// Cycle through every control from Ahoj
foreach (Object controlObject in grid.Children) {
if (controlObject is TextBox) {
TextBox
textBox = controlObject as TextBox;
// Do your stuff here...
}
}
} catch (Exception exception) {
// Unable to catch or cast the object
}
EDIT: If you also need to know the position of each TextBox
you may use a for(;;)
instead.
EDIT 15 Jan 2016: Here is the code updated based on what's been discused on comments and with the sudden realization that you could simply get the List
binded to the control since the beginning:
try {
List<data>
dataList = br.ItemsSource;
/*
Do your stuff here ...
*/
} catch(Exception exception) {
// Unable to get the previously binded items
}
Upvotes: 1
Reputation: 341
Not sure if this is what you are asking but if you want to create a list of all the strings in the textboxes you can do the following:
Loop through each visual child element of the "Ahoj" grid (using VisualTreeHelper.GetChild(container, index)) and check if it is a TextBox Type. In case it is request the Text property of the TextBox and add it to the list of strings.
See MSDN VisualTreeHelper for more info.
Upvotes: 1