2016-12-07 6 views
0

私はItemsControl内に存在する1つまたは複数のグリッドでアニメーションを行う必要があります。マイの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のサイクルでそれを使用することができますあなたはそれをしたいですか?実際に達成しようとしていることを説明してください。
関連する問題