2017-04-27 8 views
0

ソースxmlに書き戻すためにネストされたスタックパネルからテキストボックスにアクセスする方法を教えてください。ネストされたスタックパネルからデータを取得する

スタックパネルを表示しやすくするため、また結果を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-sharp-finding-controls-panel-inside-a-panel/17750097#17750097 – TripleD

答えて

0

私はこの記事で答えを見つけ探し続けて:ディックSchuermanがあるが、それは完全に働いた誰にWPF C# Finding controls from panel inside a panel

私の感謝を!

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 
    } 
} 
関連する問題