我是pivot和wp8的新手。我想从动态数据透视表项中访问某些内容的值。这是我的动态枢轴项目
for (var i = 0; i < Globals.quizcount; i++)
{
var count = i + 1;
if (count == Globals.quizcount)
{
var stackpanel = new StackPanel();
var textblok = new TextBlock { Text = o["questions"][i]["question"].ToString(), FontSize = 20, Width = 450 };
textblok.TextWrapping = TextWrapping.Wrap;
stackpanel.Children.Add(textblok);
var radio = new RadioButton { Content = "Yes", GroupName = "userans", Name = "ansYes" + count };
stackpanel.Children.Add(radio);
var radio1 = new RadioButton { Content = "No", GroupName = "userans", Name = "ansNo" + count };
stackpanel.Children.Add(radio1);
var button = new Button { Content = "Submit", Name = "submitQuiz" };
button.Click += new RoutedEventHandler(getAnswer);
stackpanel.Children.Add(button);
quizPivot.Items.Add(new PivotItem { Name = "question" + count, Header = "Question " + count, Content = stackpanel });
//quesId.Text = o["questions"][i]["_id"].ToString();Click="Button_Click"
}
else
{
var stackpanel = new StackPanel();
var textblok = new TextBlock { Text = o["questions"][i]["question"].ToString(), FontSize = 20, Width = 450 };
textblok.TextWrapping = TextWrapping.Wrap;
stackpanel.Children.Add(textblok);
var radio = new RadioButton { Content = "Yes", GroupName = "userans", Name = "useransYes" + count };
stackpanel.Children.Add(radio);
var radio1 = new RadioButton { Content = "No", GroupName = "userans", Name = "useransNo" + count };
stackpanel.Children.Add(radio1);
//, HorizontalAlignment = "Left", Margin = "66,317,0,0", VerticalAlignment = "Top
quizPivot.Items.Add(new PivotItem { Name = "question" + count, Header = "Question " + count, Content = stackpanel });
// quesId.Text = o["questions"][i]["_id"].ToString();
}
}
我不知道如何获取文本块的值并检查选中了哪个单选按钮。需要帮助
谢谢
答案 0 :(得分:0)
您可以尝试以这种方式访问动态创建的TextBlock
和RadioButton
:
//get stackpanel from the first pivot item
var pivotItem = (PivotItem)quizPivot.Items[0];
var stackpanel = (StackPanel)pivotItem.Content;
foreach(var control in stackpanel.Children)
{
if(control is RadioButton)
{
var radio = (RadioButton)control;
//do something with RadioButton
}
else if(control is TextBlock)
{
var textblok = (TextBlock)control;
//do something with TextBlock
}
}
上面的示例演示了如何从第一个透视项(quizPivot.Items[0]
)获取控件。这基本上与您从C#代码创建控件时所做的相反。