2011-11-13 75 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

很樂意幫忙!如果它解決了你的問題,你可以接受它作爲答案:) –

+0

嘿,ListBox.Items有15項,但我只得到5項,這怎麼會發生? –

相關問題