1

我希望添加到我的 ItemsControl 的项目在添加时为高度设置动画。以下示例使用触发器完成了这项工作,但我无法让它与项目的非固定高度(在本例中为 50)一起工作。

<ItemsControl ItemsSource="{Binding Notifications}">
<ItemsControl.Resources>
    <DataTemplate DataType="{x:Type Notifications1:Notification}">
        <Button x:Name="ItemButton"
                ClipToBounds="True"
                Height="0">
            <Button.Template>
                <ControlTemplate>
                    <Notifications:NotificationTile />
                </ControlTemplate>
            </Button.Template>
        </Button>
        <DataTemplate.Triggers>
            <EventTrigger RoutedEvent="FrameworkElement.Loaded"
                            SourceName="ItemButton">
                <BeginStoryboard>
                    <Storyboard>
                        <DoubleAnimation Storyboard.TargetProperty="Height"
                                            Storyboard.TargetName="ItemButton"
                                            Duration="0:0:0.5"
                                            To="50" />
                    </Storyboard>
                </BeginStoryboard>
            </EventTrigger>
        </DataTemplate.Triggers>
    </DataTemplate>
</ItemsControl.Resources>

然后我尝试使用 VisualStateManager 来执行动画,这样图块就会增长到它们需要的任何高度。在下面的示例中,项目以正确的大小添加,但不执行动画。我假设 EventTrigger 甚至没有被解雇?

任何想法都非常感谢!

<ItemsControl ItemsSource="{Binding Notifications}"
            Width="230"
            ScrollViewer.HorizontalScrollBarVisibility="Hidden"
            HorizontalContentAlignment="Stretch">

<ItemsControl.Resources>
    <DataTemplate DataType="{x:Type Notifications1:Notification}">
        <Button x:Name="ItemButton"
                ClipToBounds="True"
                Command="{Binding DataContext.ItemClicked, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"
                CommandParameter="{Binding}"
                Visibility="{Binding IsVisible, Converter={StaticResource boolToVisibilityConverter}}">
            <Button.Template>
                <ControlTemplate>
                    <Notifications:NotificationTile />
                </ControlTemplate>
            </Button.Template>

            <VisualStateManager.CustomVisualStateManager>
                <is:ExtendedVisualStateManager />
            </VisualStateManager.CustomVisualStateManager>
            <VisualStateManager.VisualStateGroups>
                <VisualStateGroup is:ExtendedVisualStateManager.UseFluidLayout="True">
                    <VisualStateGroup.Transitions>
                        <VisualTransition GeneratedDuration="0:0:2" />
                    </VisualStateGroup.Transitions>
                    <VisualState x:Name="Collapsed">
                        <Storyboard>
                            <DoubleAnimation Storyboard.TargetProperty="(FrameworkElement.Height)"
                                                Storyboard.TargetName="ItemButton"
                                                Duration="0"
                                                To="0" />
                        </Storyboard>
                    </VisualState>
                    <VisualState x:Name="Expanded">
                        <Storyboard>
                            <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Height)"
                                                            Storyboard.TargetName="ItemButton">
                                <DiscreteDoubleKeyFrame KeyTime="0"
                                                        Value="NaN" />
                            </DoubleAnimationUsingKeyFrames>
                        </Storyboard>
                    </VisualState>
                </VisualStateGroup>
            </VisualStateManager.VisualStateGroups>

            <i:Interaction.Triggers>
                <i:EventTrigger SourceName="ItemButton"
                                EventName="(FrameworkElement.Loaded)">
                    <is:GoToStateAction StateName="Expanded" />
                </i:EventTrigger>
            </i:Interaction.Triggers>
        </Button>
    </DataTemplate>
</ItemsControl.Resources>

4

1 回答 1

0

也许这有点离题,对此感到抱歉。我不确定我是否完全理解你的问题。无论如何,一般不建议使用只有“To”的动画,因为“From”最初设置为 Double.NaN,而 DoubleAnimation 在这种情况下不起作用。

于 2012-08-15T11:04:20.387 回答