Grid中的Storyboard是Listbox Itemtemplate的一部分,不是动画

时间:2015-12-13 15:19:00

标签: c# xaml listbox windows-phone-8.1 storyboard

我有一个位于网格资源内的故事板。网格在Listbox中,我可以找到故事板并调用storyboard.Begin(),但是当我这样做时没有任何反应,即使应该这样做。这是我到目前为止所得到的:

<ListBox x:Name="Defense_Units_LB" Margin="168,92,471,83" ItemsSource="{Binding DefendersUnits}">
        <ListBox.ItemTemplate>
            <DataTemplate x:Name="Def_Datatemplate">
                <Grid x:Name="Grid_animation" Background="Green">
                    <Grid.Resources>
                        <Storyboard x:Key="test_stb">
                            <ColorAnimation Storyboard.TargetName="Grid_animation" Storyboard.TargetProperty="(Grid.Background).(SolidColorBrush.Color)" Duration="0:0:1" To="Red"/>
                        </Storyboard>
                    </Grid.Resources>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*"/>
                        <ColumnDefinition Width="100"/>
                    </Grid.ColumnDefinitions>

                    <Viewbox Grid.Column="0" Height="50" Width="50" Stretch="Uniform">
                        <Units:UnitSelector  DataContext="{Binding}"/>
                    </Viewbox>
                    <Rectangle Fill="#FFF7C85F" HorizontalAlignment="Left" Height="16" Margin="1,32,0,0" Stroke="#FF000000" StrokeLineJoin="Round" VerticalAlignment="Top" Width="33" Grid.Column="1"/>
                    <TextBlock x:Name="Amount" HorizontalAlignment="Left" Height="16" TextWrapping="Wrap" Text="{Binding Amount}" VerticalAlignment="Top" Width="33" FontFamily="Andalus" FontSize="21.333" Foreground="#FF004F24" TextAlignment="Center" Margin="1,32,0,0" Grid.Column="1"/>
                    <Rectangle x:Name="rectangle1" Grid.Column="1" Margin="53,0,0,0" StrokeLineJoin="Round">
                        <Rectangle.Fill>
                            <RadialGradientBrush>
                                <GradientStop Color="White" Offset="0"/>
                                <GradientStop Color="Transparent" Offset="1"/>
                            </RadialGradientBrush>
                        </Rectangle.Fill>
                    </Rectangle>
                    <TextBlock x:Name="Units_Killed" HorizontalAlignment="Right" Height="19" TextWrapping="Wrap" Text="{Binding Lost}" VerticalAlignment="Top" Width="33" FontFamily="Andalus" FontSize="14.667" Foreground="#FF900000" Margin="51,16,0,0" Grid.Column="1" Opacity="0"/>
                    <Rectangle x:Name="rectangle" Grid.Column="1" Fill="#FF900000" HorizontalAlignment="Left" Height="3" Margin="53,24,0,0" StrokeLineJoin="Round" VerticalAlignment="Top" Width="14" Opacity="0"/>
                    <!--<Path Data="M54.5312,22.8125 L65.157,22.8125" Fill="#FFF7C85F" HorizontalAlignment="Left" Height="3" Margin="53.954,24.562,0,0" Stretch="Fill" Stroke="#FFA70000" StrokeThickness="2" StrokeLineJoin="Round" UseLayoutRounding="False" VerticalAlignment="Top" Width="8.126" StrokeEndLineCap="Triangle" StrokeStartLineCap="Triangle" Grid.Column="1"/>-->
                </Grid>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

我从后面的代码中找到了故事板:

 private void Fight_Button_Click(object sender, RoutedEventArgs e)
    {
        Damage_FadeIn.Begin();
        ListBoxItem LBI = Defense_Units_LB.ItemContainerGenerator.ContainerFromIndex(0) as ListBoxItem;
        Grid Gr = FindDescendant<Grid>(LBI);
        System.Windows.Media.Animation.Storyboard stb = Gr.Resources["test_stb"] as System.Windows.Media.Animation.Storyboard;
        //System.Windows.Media.Animation.Storyboard stb = Gr.Resources["storyboard_UKF"] as System.Windows.Media.Animation.Storyboard;
        //System.Windows.Media.Animation.Storyboard stb1 = Gr.Resources["storyboard_DFU"] as System.Windows.Media.Animation.Storyboard;
        string name = Gr.Name;
        stb.Begin();
       // stb1.Begin();

    }

任何人都知道调用storyboard.begin()函数时为什么没有发生?

0 个答案:

没有答案