【发布时间】:2017-04-27 08:46:52
【问题描述】:
您能否帮助我了解如何从嵌套堆栈面板访问文本框以写回我的源 xml。
我正在动态创建堆栈面板,因为它更易于显示,而且结果可以是 1 行或 50 行。 源是一个反序列化的 XML 文件,但我只需要处理其中的 1 个部分,即 Children[7],这始终是固定的。
一旦写入堆栈面板,用户可以调整文本框中的文本,然后我可以将其读回并适当地调整 XML 源。
这是我填充堆栈面板的方式,但是您可以看到有多个实例,因此有多个堆栈面板子项:
XmlDoc = Deserialize();
if(XmlDoc != null)
{
var docWindow = new Window();
docWindow.Width = 900;
docWindow.Height = 600;
var stackPanelMain = new StackPanel { Orientation = Orientation.Vertical, Margin = new Thickness(10,10,10,10) };
stackPanelMain.Children.Add(new Label { Content = XmlDoc.Sections.Field.Children[7].Name, FontSize = 20 });
foreach(var value in XmlDoc.Sections.Field.Children[7].Instances)
{
var stackPanel = new StackPanel { Orientation = Orientation.Vertical };
stackPanel.Children.Add(new Label { Content = value.Children[0].TextValue.Text });
var stackPanelFields = new StackPanel { Orientation = Orientation.Horizontal };
stackPanelFields.Children.Add(new TextBox { Width = 200, Height = 40, Text = value.Children[1].TextValue.Text});
stackPanelFields.Children.Add(new Image { Width = 200, Height = 40 }); // yet to be created
stackPanelFields.Children.Add(new TextBox { Width = 200, Height = 40, Text = string.Format("{0}/{1}/{2}", value.Children[2].TextValue.Text, value.Children[3].TextValue.Text, value.Children[4].TextValue.Text) });
stackPanelFields.Children.Add(new Image { Width = 200, Height = 40 }); //yet to be created
stackPanel.Children.Add(stackPanelFields);
stackPanelMain.Children.Add(stackPanel);
}
docWindow.Content = stackPanelMain;
}
在用户调整文本后,我需要将其写回 xml 文件,以便遍历所有文本框,然后开始写回值。 这就是我坚持如何访问所有孩子的所有文本框的地方,我在看这样的东西:
int xmlInstance = 0;
foreach (var tb in stackPanelMain.Children.OfType<TextBox>())
{
XmlDoc.Sections.Field.Children[7].Instances[xmlInstance].Children[1].TextValue.Text = tb.Text;
XmlDoc.Sections.Field.Children[7].Instances[xmlInstance].Children[1].TextValue.IsVerified = true;
xmlInstance++;
}
但这只是与 1 个孩子打交道。
如何访问堆栈面板中的所有子文本框?或者确实,如果有更好的方法来显示和检索这些数据,我会非常感谢指针。
提前致谢。
【问题讨论】:
-
想我可能终于在这里找到了答案,进行了一些搜索,但这对我有用:stackoverflow.com/questions/17745505/…
标签: stackpanel