从嵌套的堆栈面板中重新获取数据

时间:2017-04-27 08:46:53

标签: stackpanel

您是否能够帮助我如何从嵌套的堆栈面板访问文本框以写回我的源xml。

我动态创建堆叠面板,因为它更容易显示,因为结果可以是1行或50行。 源代码是一个已被反序列化的XML文件,但我只需处理其中1个部分,即Children [7],这一直是固定的。

一旦写到stackpanel,想法是用户可以调整文本框中的文本然后我可以读回来并适当调整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个孩子。

如何访问stackpanels中的所有子文本框?或者,如果有更好的方式来显示和检索这些数据,我非常感谢指针。

提前致谢。

1 个答案:

答案 0 :(得分:0)

我的继续搜索在这篇文章中找到了答案:WPF C# Finding controls from panel inside a panel

我要感谢Dick Schuerman,但它完美无缺!

 class ChildControls
    {
        private List<object> lstChildren;

        public List<object> GetChildren(Visual p_vParent, int p_nLevel)
        {
            if (p_vParent == null)
            {
                throw new ArgumentNullException("Element {0} is null!", p_vParent.ToString());
            }

            this.lstChildren = new List<object>();

            this.GetChildControls(p_vParent, p_nLevel);

            return this.lstChildren;

        }

        private void GetChildControls(Visual p_vParent, int p_nLevel)
        {
            int nChildCount = VisualTreeHelper.GetChildrenCount(p_vParent);

            for (int i = 0; i <= nChildCount - 1; i++)
            {
                Visual v = (Visual)VisualTreeHelper.GetChild(p_vParent, i);

                lstChildren.Add((object)v);

                if (VisualTreeHelper.GetChildrenCount(v) > 0)
                {
                    GetChildControls(v, p_nLevel + 1);
                }
            }
        }
    }

你这样使用它:

    ChildControls ccChildren = new ChildControls();

foreach (object o in ccChildren.GetChildren(WrapPanelTest, 5))
{
    if (o.GetType() == typeof(TextBox))
    {
        // Do something
    }
}