1

所以我试图在从 ItemsControl 删除项目期间制作一些动画,该项目附加到ObservableCollectio<Item> 我知道我不能在卸载事件中执行此操作,因为执行任何动画都为时已晚,所以我尝试使用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但没有任何反应。

我究竟做错了什么?

4

1 回答 1

6

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

              <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>
于 2013-09-19T10:49:11.863 回答