开发者

wp7 / Silverlight] How to animate a ListBoxItem on SelectionChanged?

I'm developing an app (VisualStudio 2010 Express for Windows Phone). I have a listbox with images and a storyboard with an animation (a projection) that I want to apply to a specific listboxitem/image when the SelectionChanged event gets fired (well not immediatly but inside the event handler).

开发者_运维百科

How can I "link" my animation to this specific ListBoxItem?


well, after try and error i came up with a solution, but it's not exactly what i wanted (the storyboard defined outside the datatemplate and maybe less code. I think it's too much for just flipping an image), but very close.

so, the example listbox:

<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>

And the Code behind:

    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();
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜