2016-12-07 16 views
0

ItemsControl에있는 하나 이상의 그리드로 애니메이션을 수행해야합니다. 내 ItemsControl에ItemsControl 내의 모든 컨트롤 찾기 - WPF

<StackPanel x:Name="SlideMainContent" Orientation="Vertical"> 
      <ItemsControl Name="itemControls"> 
       <ItemsControl.ItemTemplate> 
        <DataTemplate> 
         <Grid Tag="{Binding Path=(ItemsControl.AlternationIndex), RelativeSource={RelativeSource Mode=TemplatedParent}, FallbackValue=FAIL, StringFormat={}grid{0}}" Width="{Binding ActualWidth, ElementName=SlideMainViewer}" 
           Height="{Binding ElementName=SlideMainViewer, Path=ActualHeight}"> 
          <Grid.RowDefinitions> 
           <RowDefinition Height=".3*" /> 
           <RowDefinition Height="auto"/> 
           <RowDefinition Height="auto" /> 
           <RowDefinition Height="*" /> 
           <RowDefinition Height=".3*" /> 
          </Grid.RowDefinitions> 
          <Grid.ColumnDefinitions> 
           <ColumnDefinition Width="*"/> 
           <ColumnDefinition Width=".2*"/> 
           <ColumnDefinition Width="2*" /> 
          </Grid.ColumnDefinitions> 
          <Border Grid.Row="1" Grid.RowSpan="3" VerticalAlignment="Top" Grid.Column="0" BorderThickness="5" BorderBrush="White"> 
           <Image Stretch="Uniform" Source="{Binding Path=ImageURL}"/> 
          </Border> 
          <TextBlock Grid.Row="1" Grid.Column="2" FontFamily="{StaticResource AvenirLT65}" Style="{StaticResource HeaderStyle}" Text="{Binding (ItemsControl.AlternationIndex), RelativeSource={RelativeSource AncestorType=StackPanel}}" /> 
          <TextBlock Grid.Row="2" Grid.Column="2" FontFamily="{StaticResource AvenirLT65}" Style="{StaticResource SubHeaderStyle}" Margin="0 10" Text="{Binding Path=NewsDate}" /> 
          <TextBlock Grid.Row="3" Grid.Column="2" FontFamily="{StaticResource AvenirLT35}" Style="{StaticResource TextStyle}" Text="{Binding Path=Description}" /> 
         </Grid> 
        </DataTemplate> 
       </ItemsControl.ItemTemplate> 
      </ItemsControl> 
    </StackPanel> 

여기 내가 좋아하는 내 애니메이션에 대한 모든 그리드 패널을 얻을 사용하려면, 같은

foreach (Grid grd in SlideMainContent.Children) 
{ 
    // my code comes here..... 
} 

이다 그러나 나는 모든 격자를 얻을 수 있습니다.

+1

왜 것 여기

itemControls.UpdateLayout(); foreach(var item in itemControls.Items) { var parentObject = item.ItemContainerGenerator.ContainerFromItem(item); Grid grid = FindChild<Grid>(parentObject); ... your code here ... } 
Clemens

+0

내 애니메이션에 모든 그리드를 사용하고 싶습니다. – ganesh

답변

1

VisualTreeHelper를 사용할 수 있습니다.

삽입 코드 숨김에이 방법 : 당신이 그것을주는 유형의 첫 번째 자식 컨트롤을 찾는 데 도움이됩니다

public static T FindChild<T>(DependencyObject parent) where T : DependencyObject 
    { 
     if (parent != null) 
     { 
      for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++) 
      { 
       DependencyObject child = VisualTreeHelper.GetChild(parent, i); 
       if (child != null && child is T) 
       { 
        return (T) child; 
       } 

       T childItem = FindChild<T>(child); 
       if (childItem != null) 
       { 
        return childItem; 
       } 
      } 
     } 
     return null; 
    } 

.

public static List<T> GetChildrenOfType<T>(this DependencyObject depObj) 
    where T : DependencyObject 
    { 
     var result = new List<T>(); 
     if (depObj == null) return null; 
     var queue = new Queue<DependencyObject>(); 
     queue.Enqueue(depObj); 
     while (queue.Count > 0) 
     { 
      var currentElement = queue.Dequeue(); 
      var childrenCount = VisualTreeHelper.GetChildrenCount(currentElement); 
      for (var i = 0; i < childrenCount; i++) 
      { 
       var child = VisualTreeHelper.GetChild(currentElement, i); 
       if (child is T) 
        result.Add(child as T); 
       queue.Enqueue(child); 
      } 
     } 

     return result; 
    } 

가능한 사용 :

someItemsControl.GetChildrenOfType<FrameworkElements>(); 
1

특정 유형의 모든 아이들을 찾기 위해 확장 방법 : 다음과 같이 당신은 당신의 foreach는 사이클에서 사용할 수 있습니다 너 그렇게하고 싶어? 실제로 달성하고자하는 것을 설명하십시오.