2013-10-10 1 views
0

동적으로 목록 상자를 만들었지 만 이제는 WPF를 통해 모든 것을 엄격하게 처리하고 있습니다. 왜 .. 나에게 맞는 것 같아서 내가 뭔가를 놓친 것 같아?Listbox 내 StackPanels의 자식에 액세스 할 수없는 것 같습니다

내가지고있어 오류가

WPF 코드 "개체 참조가 개체의 인스턴스로 설정되지 않았습니다"입니다 :

    <ListBox x:Name="ListBoxT10"> 
         <ListBoxItem> 
          <StackPanel Orientation="Horizontal">          
           <TextBox FontSize="14" Text="" Width="120"/> 
           <TextBox FontSize="14" Text="" Width="40" MaxLength="3"/> 
           <TextBox FontSize="14" Text="" Width="413"/> 
          </StackPanel> 
         </ListBoxItem> 
         ...a bunch of the same listboxitems... 

C# 코드 :

  int a = 0; 
      foreach (var item in ListBoxT10.Items) 
      { 
       StackPanel tempStackPanel = item as StackPanel; 

       Console.WriteLine(tempStackPanel.Children.Count); //this even errors.. i guess it's not finding the children 

       string pName = (tempStackPanel.Children[0] as TextBox).Text; //error 
       string tri = (tempStackPanel.Children[1] as TextBox).Text; //error 
       string stats = (tempStackPanel.Children[2] as TextBox).Text; //error 

편집 : 그것은 ListBoxT10.Items의 항목이 실제로 "ListBoxItem" 's입니다. 그러나 이제는 listboxitems의 하위 항목을 가져올 수 없기 때문입니다. ugh = [

답변

0
   ListBoxItem lbi = (ListBoxItem)(ListBoxT10.ItemContainerGenerator.ContainerFromIndex(a)); 
       StackPanel sp = lbi.Content as StackPanel; 

       string pName = (sp.Children[1] as TextBox).Text; 
       string tri = (sp.Children[2] as TextBox).Text; 
       string stats = (sp.Children[3] as TextBox).Text; 

listboxitem을 잊어 버렸습니다 ......... 너무 개체로 등록 할 것이라고 생각하지 않았습니다

관련 문제