wp7 / Silverlight]如何在SelectionChanged上为ListBoxItem设置动画?

时间:2010-10-25 16:17:16

标签: silverlight windows-phone-7

我正在开发一款应用程序(适用于Windows Phone的VisualStudio 2010 Express)。 我有一个包含图像的列表框和一个带有动画的故事板(投影),当SelectionChanged事件被触发时我想要应用于特定的listboxitem / image(当然不是在事件处理程序内部。)

如何将动画“链接”到此特定的ListBoxItem?

1 个答案:

答案 0 :(得分: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();
    }