2012-07-03 3 views
0

Windows-phone에있는 다음 코드를 사용하여 템플릿의 ItemsPanel을 읽습니다. 하지만 그것은 나를 위해 작동하지 않습니다 .. 항상 ChildrenCount 0을 반환합니다.VisualTreeHelper가 CustomControl에서 Childrens를 가져 오지 못합니다

private void OnPropertyChanged(DependencyPropertyChangedEventArgs e) 
    { 
     StackPanel stackpanel = TraverseVisualTree<StackPanel>(this); 
    } 

    private T TraverseVisualTree<T>(DependencyObject visual) where T : DependencyObject 
    { 
     var itemscount = VisualTreeHelper.GetChildrenCount(visual); 

     for (int i = 0; i < itemscount; i++) 
     { 
      var child = VisualTreeHelper.GetChild(visual, i); 
      if (child is T) 
       return (T)child; 
     } 
     return null; 
     } 

아무쪼록 도와 주시겠습니까?

답변

-1

사용자 지정 컨트롤에서 아이를 얻기 위해, 아래와 같이

CustomControl: 

<UserControl x:Class='SilverlightApplication3.MainPage' 
    xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' 
    xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' 
    xmlns:d='http://schemas.microsoft.com/expression/blend/2008' 
    xmlns:mc='http://schemas.openxmlformats.org/markup-compatibility/2006' 
    mc:Ignorable='d' 
    d:DesignWidth='640' 
    d:DesignHeight='480'> 

     <StackPanel x:Name="LayoutRoot" Background="White"> 

      <Button x:Name="btnWalkTree" 
       Content="Walk Tree" 
       Click="btnWalkTree_Click"/> 

      <Border CornerRadius="10" Background="Yellow"> 
       <TextBlock x:Name="tbName1" 
        Text="Santa Clause" 
        HorizontalAlignment="Center" 
        VerticalAlignment="Center" 
        Margin="3" /> 
      </Border> 

      <Border CornerRadius="10" Background="AliceBlue"> 
       <TextBlock x:Name="tbName2" 
         Text="Mickey Mouse" 
         HorizontalAlignment="Center" 
         VerticalAlignment="Center" 
         Margin="3" /> 
      </Border> 

      <Border CornerRadius="10" Background="Green"> 
       <TextBlock x:Name="tbName3" 
         Text="The Tooth Fairy" 
         HorizontalAlignment="Center" 
         VerticalAlignment="Center" 
         Margin="3" /> 
      </Border> 

     </StackPanel> 

</UserControl> 

코드를보십시오 :

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Net; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Documents; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Animation; 
using System.Windows.Shapes; 

namespace SilverlightApplication3 
{ 
    public partial class MainPage : UserControl 
    { 
     public MainPage() 
     { 
      InitializeComponent(); 
     } 

     private void btnWalkTree_Click(object sender, RoutedEventArgs e) 
     { 
      this.WalkChildren(this); 
     } 

     private void WalkChildren(DependencyObject depObject) 
     { 
      string name = String.Empty; 
      FrameworkElement element = depObject as FrameworkElement; 
      if (element != null) 
      { 
       name = element.Name; 
      } 

      int childCount = VisualTreeHelper.GetChildrenCount(element); 
      if (childCount > 0) 
      { 
       for (int i = 0; i < childCount; i++) 
       { 
        this.WalkChildren(VisualTreeHelper.GetChild(element, i)); 
       } 
      } 
     } 
    } 
} 

내가 ItemsPanelTemplate에서 읽어 물었다 VisualTreeHelper

+0

에서 참조 된 .. 나는 이걸 이미 알 거라 .. –

+0

질문에이 점을 언급해야합니다 ... – Ponmalar

관련 문제