Windows phone 8.1 - 来自动态创建的文本框的文本
Windows phone 8.1 - text from dynamically created textBoxes
我正在尝试在 Windows phone 8.1 中创建 table 方案,但我无法保存它。我在 XAML 中创建了 table:这是代码
<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>
但我不知道如何从这个动态创建的文本框中读取值。我需要获取所有文本框的值。我不需要单独与他们合作。
谢谢
编辑
我尝试使用答案中的代码并且效果很好,但仅限于第一个网格。
我也在动态创建网格。此网格与 Binding 具有相同的名称,但具有不同的值。
答案中的代码仅从第一行文本框返回值...
代码 - 我将项目添加到列表中,在 Itemsource 之后是这个列表,我将它绑定到文本框
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数据
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;
}
}
不确定这是否是您要问的,但如果您想创建文本框中所有字符串的列表,您可以执行以下操作:
遍历 "Ahoj" 网格的每个可视子元素(使用 VisualTreeHelper.GetChild(container, index))并检查它是否为 TextBox 类型。如果请求 TextBox 的 Text 属性 并将其添加到字符串列表中。
有关详细信息,请参阅 MSDN VisualTreeHelper。
我也处理过动态内容,我曾经这样做过:(根据您的要求进行调整)
// 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...
}
}
既然你说你不能直接访问 Grid
(如果我理解正确的话,你是通过 run-time 代码添加控件)那么你可以这样做:
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
}
编辑: 如果您还需要知道每个 TextBox
的位置,您可以使用 for(;;)
代替。
2016 年 1 月 15 日编辑: 这是根据评论中讨论的内容更新的代码,突然意识到您可以简单地将 List
绑定到从头开始的控制:
try {
List<data>
dataList = br.ItemsSource;
/*
Do your stuff here ...
*/
} catch(Exception exception) {
// Unable to get the previously binded items
}
我正在尝试在 Windows phone 8.1 中创建 table 方案,但我无法保存它。我在 XAML 中创建了 table:这是代码
<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>
但我不知道如何从这个动态创建的文本框中读取值。我需要获取所有文本框的值。我不需要单独与他们合作。 谢谢
编辑
我尝试使用答案中的代码并且效果很好,但仅限于第一个网格。 我也在动态创建网格。此网格与 Binding 具有相同的名称,但具有不同的值。 答案中的代码仅从第一行文本框返回值...
代码 - 我将项目添加到列表中,在 Itemsource 之后是这个列表,我将它绑定到文本框
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数据
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;
}
}
不确定这是否是您要问的,但如果您想创建文本框中所有字符串的列表,您可以执行以下操作:
遍历 "Ahoj" 网格的每个可视子元素(使用 VisualTreeHelper.GetChild(container, index))并检查它是否为 TextBox 类型。如果请求 TextBox 的 Text 属性 并将其添加到字符串列表中。
有关详细信息,请参阅 MSDN VisualTreeHelper。
我也处理过动态内容,我曾经这样做过:(根据您的要求进行调整)
// 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...
}
}
既然你说你不能直接访问 Grid
(如果我理解正确的话,你是通过 run-time 代码添加控件)那么你可以这样做:
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
}
编辑: 如果您还需要知道每个 TextBox
的位置,您可以使用 for(;;)
代替。
2016 年 1 月 15 日编辑: 这是根据评论中讨论的内容更新的代码,突然意识到您可以简单地将 List
绑定到从头开始的控制:
try {
List<data>
dataList = br.ItemsSource;
/*
Do your stuff here ...
*/
} catch(Exception exception) {
// Unable to get the previously binded items
}