2017-04-27 104 views
0

你能够帮助我如何访问从嵌套堆栈面板的文本框写回到我的源xml。从嵌套堆栈面板Retreiving数据

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

一旦写入到堆栈面板,用户可以调整文本框中的文本,然后我可以读回来并适当调整XML源。

这里是我如何填充stackpanels,但你可以看到有多个实例,那么多的StackPanel孩子:

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名儿童。

如何访问堆叠面板中的所有子文本框?或者的确,如果有更好的方式来显示和检索这些数据,我会非常欣赏一个指针。

在此先感谢。

+0

想我可能已经找到了答案终于在这里,花了一些搜索,但这个工作对我来说:http://stackoverflow.com/questions/17745505/WPF-C-尖锐调查-控制 - 从面板的内-A-面板/ 17750097#17750097 – TripleD

回答

0

我继续寻找找到了答案在这个岗位:WPF C# Finding controls from panel inside a panel

我要感谢谁迪克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 
    } 
}