WPF - 数据触发器动画不起作用

本文关键字:动画 不起作用 触发器 数据 WPF | 更新日期: 2023-09-27 18:32:01

所以我正在尝试在从附加到ObservableCollectio<Item>的 ItemsControl 中删除项目期间制作一些动画我知道我不能在卸载事件中执行此操作,因为执行任何动画都为时已晚,所以我尝试使用DataTrigger

我的xaml文件如下所示:

 <DataTemplate DataType="{x:Type MyApp:Item}">
        <Border x:Name="ItemBorder">
            <Label Content="{Binding Path=Name}" />
        </Border>
        <DataTemplate.Triggers>
            <DataTrigger Binding="{Binding Path=Removing}" Value="True">
                <DataTrigger.EnterActions>
                    <BeginStoryboard>
                        <Storyboard>
                            <DoubleAnimation Duration="0:0:1" From="1.0" To="0.0"
                                           Storyboard.TargetProperty="(Border.Opacity)" />
                        </Storyboard>
                    </BeginStoryboard>
                </DataTrigger.EnterActions>
            </DataTrigger>
        </DataTemplate.Triggers>
</DataTemplate>

我的Item课很简单:

public class Item : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged = delegate { };
    private bool removing;
    public bool Removing {
        get
        {
            return removing;
        }
        set
        {
            removing = value;
            PropertyChanged(this, new PropertyChangedEventArgs("Removing"));
        }
    }
    // same with `Name` property
}

我想通过设置item.Removing = true来启动动画,但没有任何反应。

我做错了什么?

WPF - 数据触发器动画不起作用

您必须更新动画,如下所示,即给出元素名称:

              <DataTemplate.Triggers>
                        <DataTrigger Binding="{Binding Removing}" Value="True">
                            <DataTrigger.EnterActions>
                                <BeginStoryboard>
                                    <Storyboard>
                                        <DoubleAnimation Duration="0:0:1" From="1.0" To="0.0"
                                           Storyboard.TargetProperty="Opacity" Storyboard.TargetName="ItemBorder" />
                                    </Storyboard>
                                </BeginStoryboard>
                            </DataTrigger.EnterActions>
                        </DataTrigger>
             </DataTemplate.Triggers>

尝试将动画直接放在边框样式上,如下所示:

<DataTemplate DataType="{x:Type MyApp:Item}">
    <Border x:Name="ItemBorder">
        <Label Content="{Binding Path=Name}" />
        <Border.Style>
           <Style TargetType="Border">
              <Style.Triggers>
                 <DataTrigger Binding="{Binding Path=Removing}" Value="True">
                    <DataTrigger.EnterActions>
                      <BeginStoryboard>
                        <Storyboard>
                            <DoubleAnimation Duration="0:0:1" From="1.0" To="0.0"
                                           Storyboard.TargetProperty="Opacity" />
                        </Storyboard>
                    </BeginStoryboard>
                </DataTrigger.EnterActions>
            </DataTrigger> 
          <Style.Triggers>
        </Style>         
</DataTemplate>