4

ViewModelからObservableCollectionをバインドするItemsControlを備えたWPF-Viewがあります。ここで、ObservableCollectionから削除したアイテムをゆっくりとフェードアウトしたいと思います。

ViewModel:

public class ViewModel
{
    public ObservableCollection<string> Items { get; set; }
}

意見:

<Window x:Class="Sandbox.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"
    Name="mainWindow">
<Window.Resources>
    <DataTemplate x:Key="mytemplate">
        <DataTemplate.Resources>
            <Storyboard x:Key="OnUnloaded">
                <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)" Storyboard.TargetName="grid">
                    <EasingDoubleKeyFrame KeyTime="0" Value="1"/>
                    <EasingDoubleKeyFrame KeyTime="0:0:0.3" Value="-0"/>
                </DoubleAnimationUsingKeyFrames>
                <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.Y)" Storyboard.TargetName="grid">
                    <EasingDoubleKeyFrame KeyTime="0" Value="0"/>
                    <EasingDoubleKeyFrame KeyTime="0:0:0.3" Value="-10"/>
                </DoubleAnimationUsingKeyFrames>
            </Storyboard>
        </DataTemplate.Resources>
        <Grid x:Name="grid" RenderTransformOrigin="0.5,0.5">
            <Grid.RenderTransform>
                <TransformGroup>
                    <ScaleTransform/>
                    <SkewTransform/>
                    <RotateTransform/>
                    <TranslateTransform/>
                </TransformGroup>
            </Grid.RenderTransform>
            <TextBox Text="{Binding Mode=OneWay}"/>
        </Grid>
        <DataTemplate.Triggers>
            <EventTrigger RoutedEvent="FrameworkElement.Unloaded">
                <BeginStoryboard Storyboard="{StaticResource OnUnloaded}"/>
            </EventTrigger>
        </DataTemplate.Triggers>
    </DataTemplate>
</Window.Resources>
<StackPanel>
    <ItemsControl Grid.Row="1" ItemsSource="{Binding Items}" ItemTemplate="{StaticResource mytemplate}"/>
</StackPanel>

私が抱えている問題は、ストーリーボードがコレクションからアイテムを削除することから始まることですが、同時にitemscontrolがアイテムを削除するため、アニメーションが表示されません...

アニメーションが終了する前にアイテムが削除されないようにする方法はありますか?

4

1 に答える 1

9

これは本来よりもはるかに困難です。「削除」アニメーションの問題は、アイテムがデータバインドされたコレクションから削除されると、対応するビジュアルが要素ツリーから自動的に削除されることです。これは、アニメートするものが何も残っていないことを意味します。

これを回避するには、データバインドされたコレクションからアイテムを削除する前にアニメーションをキューに入れる方法を見つける必要があります。アニメーションが完了したら、アイテムを削除してもよいことをViewModelに通知します。

もう1つの解決策は、ItemsControlの動作を変更して、コンテナービジュアルの存続期間をより適切に制御することです。

いずれにせよ、残念ながら、達成するのは簡単な作業ではありません。

于 2011-07-24T18:40:27.303 に答える