C# 如何获取文本框';(在stackpanel上)值并使用循环将它们放入数组?

C# 如何获取文本框';(在stackpanel上)值并使用循环将它们放入数组?,c#,windows-phone-8,textbox,windows-phone-8.1,C#,Windows Phone 8,Textbox,Windows Phone 8.1,我正在创建一个文本框矩阵,然后我想在这些文本框中输入一些值。之后,通过单击我的矩阵下面的按钮,程序应该会得到值(这是主要问题!),我想我可以使用foreach-UIElement实现它,但它不起作用。。。我附上一个截图和一个代码,请更正它 private void vsematrici_SelectionChanged(object sender, SelectionChangedEventArgs e) { int selectedIndex = vsematrici.Selecte

我正在创建一个文本框矩阵,然后我想在这些文本框中输入一些值。之后,通过单击我的矩阵下面的按钮,程序应该会得到值(这是主要问题!),我想我可以使用
foreach-UIElement
实现它,但它不起作用。。。我附上一个截图和一个代码,请更正它

private void vsematrici_SelectionChanged(object sender, SelectionChangedEventArgs e)
{

    int selectedIndex = vsematrici.SelectedIndex + 2;
    StackPanel[] v = new StackPanel[selectedIndex];
    for (int i = 0; i < selectedIndex; i++)
    {
        v[i] = new StackPanel();
        v[i].Name = "matrixpanel" + i;
        v[i].Orientation = Orientation.Horizontal;

        TextBox[] t = new TextBox[selectedIndex];
        for (int j = 0; j < selectedIndex; j++)
        {
            t[j] = new TextBox();
            t[j].Name = "a" + (i + 1) + (j + 1);
            t[j].Text = "a" + (i + 1) + (j + 1);
            v[i].Children.Add(t[j]);

            Thickness m = t[j].Margin;
            m.Left = 1;
            m.Bottom = 1;
            t[j].Margin = m;

            InputScope scope = new InputScope();
            InputScopeName name = new InputScopeName();
            name.NameValue = InputScopeNameValue.TelephoneNumber;
            scope.Names.Add(name);
            t[j].InputScope = scope;
        }
        mainpanel.Children.Add(v[i]);

    }
    Button button1 = new Button();
    button1.Content = "Найти определитель";
    button1.Click += Button_Click;
    mainpanel.Children.Add(button1);

}

private void Button_Click(object sender, RoutedEventArgs e)
{
    myresult.Text = "After button clicking there should be shown a matrix of texboxes values";

    foreach (UIElement ctrl in mainpanel.Children)
    {
        if (ctrl.GetType() == typeof(TextBox))
        {
            //There should be a a two-dimensional array that I want to fill with textboxes' values
            //But even this "if" doen't work!!! I don't know why...
        }
    }
}
private void vsematrici\u SelectionChanged(对象发送方,SelectionChangedEventArgs e)
{
int-selectedIndex=vsematrici.selectedIndex+2;
StackPanel[]v=新的StackPanel[selectedIndex];
对于(int i=0;i

您正在将一些
StackPanel
添加到
mainPanel
中,然后将文本框添加到该StackPanel中

但在这里:

foreach (UIElement ctrl in mainpanel.Children)
{
    if (ctrl.GetType() == typeof(TextBox))
    {
您试图找到这些文本框,因为它们是
mainPanel
的子对象-当然,您无法通过这种方式找到它们

因此,您可以根据以下逻辑更改代码:

foreach (UIElement pnl in mainpanel.Children)
{
    if (pnl is StackPanel)
    {
        foreach (UIElement ctrl in (pnl as StackPanel).Children)
        {
             if (ctrl is TextBox)
             {
               // your logic here
             }
        }
    }
}

我如何制作一个二维数组并用文本框的值填充它呢Ваш код выдаёт ошибку... :pnl.Children、pnl.Children、pnl.Children、pnl.Children、pnl.Children、pnl.Children、pnl.Children、pnl.Children、pnl.Children、pnl.Children、pnl.Children、pnl.Children.Children、pnl.Children.Children、pnl.Children.Children.Children、pnl.Children.Children、pnl.Children.Children.Children、pnl.Children.Children、pnl.Children.Children、pnl.Children.Children、pnl.Children.Children.Children.childarray=new int[selectedIndex]
(或双精度,或十进制-我不知道您需要什么确切类型),并在迭代文本框时填充它。@БББПааааааааааааааа。事实上,它应该是
(pnl作为StackPanel)。子元素作为UIElement不具有该属性。另外请注意,StackOverflow是英语资源,如果我们在这里说俄语,所有这些信息对社区的大多数人都是无用的。所以请用英语。哇!作品非常感谢你!你能推荐一本学习C的书吗?(当然是俄语书)