【问题标题】:Fade out removing item from itemscontrol淡出从项目控制中删除项目
【发布时间】:2011-07-24 09:30:19
【问题描述】:

我有一个带有 ItemsControl 的 WPF 视图,它绑定了来自 ViewModel 的 ObservableCollection。现在我想慢慢淡出我从 ObservableCollection 中删除的项目。

视图模型:

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 删除了该项目,为此,动画看不到......

知道如何防止在动画终止之前删除项目吗?

【问题讨论】:

标签: wpf datatemplate eventtrigger


【解决方案1】:

这比它应该做的要困难得多。 “删除”动画的问题在于,一旦从数据绑定集合中删除了一个项目,其对应的视觉效果就会自动从元素树中删除。这意味着没有什么可以制作动画了。

要解决这个问题,您需要找到一种方法在从数据绑定集合中删除项目之前将动画排队,并且在动画完成后通知 ViewModel 可以删除该项目。

另一种解决方案是修改 ItemsControl 的行为,以更好地控制容器视觉效果的生命周期。

不管怎样,不幸的是,这不是一项容易完成的任务。

【讨论】:

  • 感谢您的回答!我想也许在 xaml 中有更简单的方法,但我会尝试你上面描述的方式。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-10-19
  • 2018-12-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-31
  • 1970-01-01
相关资源
最近更新 更多