xamarin表单 - 获取网格中的文本框值

时间:2017-03-07 03:27:54

标签: c# xamarin.forms

我以编程方式生成包含标签和文本框的网格。代码如下所示

grid.Children.Add(new Label { Text = "ID", BackgroundColor = Color.Black, IsVisible = false, FontAttributes = FontAttributes.Bold, TextColor = Color.White, FontSize = Device.GetNamedSize(NamedSize.Small, typeof(Label)), }, 0, 1, 0, 1);
grid.Children.Add(new Label { Text = "Desc", BackgroundColor = Color.Black, FontAttributes = FontAttributes.Bold, TextColor = Color.White, FontSize = Device.GetNamedSize(NamedSize.Small, typeof(Label)), }, 1, 2, 0, 1);
grid.Children.Add(new Label { Text = "Qty", BackgroundColor = Color.Black, FontAttributes = FontAttributes.Bold, TextColor = Color.White, FontSize = Device.GetNamedSize(NamedSize.Small, typeof(Label)), }, 2, 3, 0, 1);
grid.Children.Add(new Label { Text = "Receive Qty", BackgroundColor = Color.Black, FontAttributes = FontAttributes.Bold, TextColor = Color.White, FontSize = Device.GetNamedSize(NamedSize.Small, typeof(Label)), }, 4, 6, 0, 1);

for (int i = 0; i < 10; i++)
{
    sID = e.Result[i].ID.ToString();
    sQty = e.Result[i].Qty.ToString();
    sDesc = e.Result[i].DESC1.ToString();
    if(e.Result[i].ReceivedQty.ToString() == "0" )
    {
        sReceivedQty = sQty;
    }
    else
    {
        sReceivedQty = e.Result[i].ReceivedQty.ToString();
    }

    grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Auto) });
    grid.Children.Add(new Label { Text = sID, IsVisible = false, BackgroundColor = Color.Gray, TextColor = Color.White, FontSize =  
        Device.GetNamedSize(NamedSize.Micro, typeof(Label)) }, 0, 1, i + 1, i + 2);
    grid.Children.Add(new Label { Text = sDesc, BackgroundColor = Color.Gray, TextColor = Color.White, FontSize = 
        Device.GetNamedSize(NamedSize.Micro, typeof(Label)) }, 1 2, i + 1, i + 2);
    grid.Children.Add(new Label { Text = sQty, BackgroundColor = Color.Gray, TextColor = Color.White, FontSize = 
        Device.GetNamedSize(NamedSize.Micro, typeof(Label)) }, 2, 3, i + 1, i + 2);
    grid.Children.Add(new Entry { Text = sReceivedQty, TextColor = Color.White, FontSize = 10 }, 3, 4, i + 1, i + 2);
}

我希望在每个行的文本框中获取值?请帮忙!

2 个答案:

答案 0 :(得分:1)

您无法查询Grid以获取第X行的元素,但您可以查询Grid中的元素并询问他们的位置。

//get the element at position r, c
var view = grid.Children.FirstOrDefault(v => Grid.GetRow(v) == r && Grid.GetColumn(v) == c);
var label = view as Label;
if (label != null) {
    //label.Text is the string you're looking for
}

答案 1 :(得分:0)

int index=8;
string y  = ((Label)gridName.Children.ElementAt(index)).Text;

这将带来标签的文本,即网格中的第8个视图。