Question

So I'm trying to make some animations during item removal from ItemsControl which is attached to ObservableCollectio<Item> I know I can't do this in unload event because it's simply too late to perform any animation so I've tried to do this using DataTrigger

My xaml file looks like this:

 <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>

and my Item class is simply:

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
}

I would like to start an animation by setting item.Removing = true but nothing happens.

What am I doing wrong?

Was it helpful?

Solution

You will have to update your animation as below i.e give elementname:

              <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>

OR

Try putting animation directly on your border style like below:

<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>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top