2011-11-13 8 views
0
<ListBox Name="listBoxButtons" 
     Height="700"> 
    <ListBox.ItemTemplate> 
     <DataTemplate> 
      <Border Background="{StaticResource PhoneAccentBrush}" 
        Name="border" 
        Width="432" Height="62" 
        Margin="6" Padding="12,0,0,6"> 
       <TextBlock Text="{Binding}" 
          Foreground="#FFFFFF" FontSize="26.667" 
          HorizontalAlignment="Left" 
          VerticalAlignment="Bottom" 
          FontFamily="{StaticResource PhoneFontFamilySemiBold}"/> 
       <Border.Projection> 
        <PlaneProjection RotationX="-60"/> 
       </Border.Projection> 
      </Border> 
     </DataTemplate> 
    </ListBox.ItemTemplate> 
</ListBox> 

コード:なぜProjectionプロパティがnullですか?

private void ShowAnim() 
{ 
    IEasingFunction quadraticEase = new QuadraticEase { EasingMode = EasingMode.EaseOut }; 
    Storyboard _swivelShow = new Storyboard(); 
    foreach (var item in this.listBoxButtons.Items) 
    { 
     UIElement container = listBoxButtons.ItemContainerGenerator.ContainerFromItem(item) as UIElement; 
     if (container != null) 
     { 
      Border content = VisualTreeHelper.GetChild(container, 0) as Border; 
      if (content != null) 
      { 
       DoubleAnimationUsingKeyFrames showAnimation = new DoubleAnimationUsingKeyFrames(); 

       EasingDoubleKeyFrame showKeyFrame1 = new EasingDoubleKeyFrame(); 
       showKeyFrame1.KeyTime = TimeSpan.FromMilliseconds(0); 
       showKeyFrame1.Value = -60; 
       showKeyFrame1.EasingFunction = quadraticEase; 

       EasingDoubleKeyFrame showKeyFrame2 = new EasingDoubleKeyFrame(); 
       showKeyFrame2.KeyTime = TimeSpan.FromMilliseconds(85); 
       showKeyFrame2.Value = 0; 
       showKeyFrame2.EasingFunction = quadraticEase; 

       showAnimation.KeyFrames.Add(showKeyFrame1); 
       showAnimation.KeyFrames.Add(showKeyFrame2); 

       Storyboard.SetTargetProperty(showAnimation, new PropertyPath(PlaneProjection.RotationXProperty)); 
       Storyboard.SetTarget(showAnimation, content.Projection); 

       _swivelShow.Children.Add(showAnimation); 
      } 
     } 
    } 
    _swivelShow.Begin(); 
} 

しかし:Storyboard.SetTarget(showAnimation, content.Projection)は例外をスローします。 content.Projectionnullです。どうしたの?

答えて

0

あなたは間違ってBorder要素を見ています。あなたのケースのアイテムコンテナの階層は、Border - >ContentControl - >ContentPresenter - >Border(これはあなたのものです)のようです。だからあなたはあなたが望むボーダーを見つけるためにコンテナの子階層をさらに深く理解しなければなりません。

次のコードでは、再帰的にUIElementの子を検索し、あなたはそれがどのようになる深く見ることができるように、いくつかのデバッグ出力を提供します。

private UIElement GetMyBorder(UIElement container) 
    { 
     if (container is FrameworkElement && ((FrameworkElement)container).Name == "border") 
      return container; 

     for (int i = 0; i < VisualTreeHelper.GetChildrenCount(container); i++) 
     { 
      var child = (FrameworkElement)VisualTreeHelper.GetChild(container, i); 
      System.Diagnostics.Debug.WriteLine("Found child "+ child.ToString()); 

      System.Diagnostics.Debug.WriteLine("Going one level deeper..."); 
      UIElement foundElement = GetMyBorder(child); 
      if (foundElement != null) 
       return foundElement; 
     } 
     return null; 
    } 

がそれを使用するには::それは「国境」という名前のコントロールを見つけたとき、それは停止します

Border content = (Border)GetMyBorder(container); 
+0

ああ、あなたはとてもクール感謝ハインリヒ・ウルブリヒト –

+0

助けるために喜んで!そして、それはあなたの問題を解決した場合、あなたはちょっと、ListBox.Itemsは15個のアイテムを持っている –

+0

:)答えとしてそれを受け入れるかもしれないが、私は唯一の5項目を取得し、どのようにそれが起こるのだろうか? –

関連する問題