【问题标题】:Retreiving data from nested stackpanels从嵌套堆栈面板中检索数据
【发布时间】: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 个孩子打交道。

如何访问堆栈面板中的所有子文本框?或者确实,如果有更好的方法来显示和检索这些数据,我会非常感谢指针。

提前致谢。

【问题讨论】:

标签: stackpanel


【解决方案1】:

我继续搜索在这篇帖子中找到了答案: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
    }
}

【讨论】:

    猜你喜欢
    • 2018-01-05
    • 2020-10-31
    • 2018-12-15
    • 1970-01-01
    • 1970-01-01
    • 2022-01-18
    • 1970-01-01
    • 1970-01-01
    • 2019-09-22
    相关资源
    最近更新 更多