2010-10-25 27 views
1

我正在開發一個應用程序(適用於Windows Phone的VisualStudio 2010 Express)。 當SelectionChanged事件被觸發時(不是立即但是在事件處理程序中),我有一個包含圖像的列表框和一個帶有動畫(投影)的故事板,我想將它應用於特定的listboxitem/image。wp7/Silverlight]如何動畫SelectionChanged上的ListBoxItem?

如何將我的動畫鏈接到此特定的ListBoxItem?

回答

2

好吧,嘗試和錯誤後,我想出了一個解決方案,但它不完全是我想要的(故事板在數據模板之外定義,也許代碼較少,我認爲它太多隻是翻轉圖像),但非常關。

如此,例如列表框:

<ListBox x:Name="lbxCardTable" SelectionChanged="lbxCardTable_SelectionChanged"> 

      <ListBox.ItemTemplate> 

       <DataTemplate> 

        <Grid x:Name="imgContainer"> 
         <Image x:Name="img" Source="{Binding } /> 
         <Grid.Resources> 
          <Storyboard x:Name="itemSb"> 
           <DoubleAnimationUsingKeyFrames 
            Storyboard.TargetName="imgContainer" 
            Storyboard.TargetProperty="(UIElement.Projection).(PlaneProjection.RotationY)"> 
            <EasingDoubleKeyFrame KeyTime="0" Value="0"/> 
            <EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="90"/> 
           </DoubleAnimationUsingKeyFrames> 
          </Storyboard> 
         </Grid.Resources> 
         <Grid.Projection> 
          <PlaneProjection/> 
         </Grid.Projection> 
        </Grid> 


       </DataTemplate> 
      </ListBox.ItemTemplate> 
     </ListBox> 

而後面的代碼:

private void lbxCardTable_SelectionChanged(object sender, SelectionChangedEventArgs e) { 
object selectedItem = lbxCardTable.SelectedItem; 

      ListBoxItem lbitem = (ListBoxItem)lbxCardTable.ItemContainerGenerator.ContainerFromItem(selectedItem); 


      var border =(Border) VisualTreeHelper.GetChild(lbitem, 0); 

      var mcontentcontrol =(ContentControl) VisualTreeHelper.GetChild(border, 0); 

      var contentpresenter =(ContentPresenter) VisualTreeHelper.GetChild(mcontentcontrol, 0); 

      var mgrid=(Grid)VisualTreeHelper.GetChild(contentpresenter,0); 



      Storyboard sb = mgrid.Resources["itemSb"] as Storyboard; 
      if (sb.GetCurrentState() != ClockState.Stopped) { 
       sb.Stop(); 
      } 


      sb.Begin(); 
    }