Windows phone 8.1 - 来自动态创建的文本框的文本

Posted

技术标签:

【中文标题】Windows phone 8.1 - 来自动态创建的文本框的文本【英文标题】:Windows phone 8.1 - text from dynamically created textBoxes 【发布时间】:2016-04-13 06:41:30 【问题描述】:

我正在尝试在 Windows phone 8.1 中创建表格方案,但我无法保存它。我在 XAML 中创建了表:这是代码

 <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

类数据

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;

  

【问题讨论】:

【参考方案1】:

我也处理过动态内容,我曾经这样做过:(适应您的要求)

// 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(如果我理解正确,您是通过运行时代码添加控件),那么您可以执行以下操作:

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 日编辑:这是根据 cmets 上讨论的内容更新的代码,突然意识到您可以简单地将 List 从一开始就绑定到控件:

try 
    List<data>
        dataList = br.ItemsSource;

    /*
        Do your stuff here ...
    */
 catch(Exception exception) 
    // Unable to get the previously binded items

【讨论】:

谢谢,它运行良好,但此代码仅适用于第一个网格...我需要来自我创建的所有网格的文本。我正在创建大约 20 个网格(名称相同,但 textBoxes 中的值不同)(我编辑了我的帖子) 你能展示你添加网格的部分代码吗?我 100% 确定您不能在同一页面上添加具有相同名称的控件。 请澄清一件事:您将多个 ItemsControl 添加到一个,比如说,StackPanel 或类似的控件,对吧?如果你是那样的人,为什么不简单地循环控制孩子呢?如果您确实在做我所说的事情,请说出来,我会用可能有效的解决方案更新我的答案。 是的,你是对的......抱歉描述有误。 感谢您的建议,但这不能解决我的问题。使用此代码,我从 ItemSource 获取原始值,但表是可编辑的,用户可以更改表中的所有值,因此我需要获取实际值。例如:第一行将是:“测试,测试,测试” - 用户将其更改为:“测试,示例,今天”并且使用 ItemSource 到列表我得到原始值“测试,测试,测试”未更改值【参考方案2】:

不确定这是否是您所要求的,但如果您想创建文本框中所有字符串的列表,您可以执行以下操作:

遍历“Ahoj”网格的每个可视子元素(使用 VisualTreeHelper.GetChild(container, index))并检查它是否为 TextBox 类型。如果是请求 TextBox 的 Text 属性并将其添加到字符串列表中。

请参阅MSDN VisualTreeHelper 了解更多信息。

【讨论】:

A 试过了,但我无法访问这个名为“ahoj”的网格,因为它在数据模板中。代码是:IEnumerable collection = ahoj.Children.OfType(); (但我无法访问此网格...)

以上是关于Windows phone 8.1 - 来自动态创建的文本框的文本的主要内容,如果未能解决你的问题,请参考以下文章

可以运行 2 个后台任务 - Windows Phone 8.1 通用应用程序?

在 windows phone 8.1 C# 中显示 &#8211 带有笑脸的文本(笑脸看起来像 &#8211。)

xml 适用于Windows 8.1和Windows Phone 8.1应用程序的扩展器控件演示。

xml 适用于Windows 8.1和Windows Phone 8.1应用程序的扩展器控件的模板。

如何将 Windows 商店中的 Windows Phone 应用程序(8.1 XAML)迁移到 8.1 Silverlight?

csharp 在Windows 8.1和Windows Phone 8.1应用程序中运行的Expander控件的代码隐藏。