2017-04-27 78 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 
    } 
}