Search code examples
c#.netwpfmvvm-light

Execute Animation DataTrigger with binding to Command


I tried to execute animation with DataTrigger but it don't reise up. Program don't throw any exception and errors. Can someone explain why animation don't Start ?

Here is my storyboard

 <Storyboard x:Key="storyboard2" AutoReverse="True" RepeatBehavior="Forever" Duration="00:01:00">

        <DoubleAnimation Storyboard.TargetProperty="(Rectangle.RenderTransform).(ScaleTransform.ScaleX)" Storyboard.Target="{Binding Source={x:Reference RectangleScaleFrom}}" To="4" From="1"/>           
        <DoubleAnimation  Storyboard.TargetProperty="(Rectangle.RenderTransform).(ScaleTransform.ScaleY)" Storyboard.Target="{Binding Source={x:Reference RectangleScaleFrom}}"
                           From="4" To="1"/>
    </Storyboard>" 

Here Is DataTrigger to start and Stop Animation

 <DataTrigger Binding="{Binding RunAnimation}" Value="True" x:Key="Start">
        <DataTrigger.EnterActions>
            <BeginStoryboard x:Name="BeginScaleFrom" Storyboard="{StaticResource storyboard2}"/>
            <!--<BeginStoryboard x:Name="BeginScaleTo" Storyboard="{StaticResource storyboard2}"/>-->
        </DataTrigger.EnterActions>
    </DataTrigger>
    <DataTrigger Binding="{Binding RunAnimation}" Value="False" x:Key="Stop">
        <DataTrigger.ExitActions>
            <StopStoryboard BeginStoryboardName="BeginScaleFrom"/>
            <!--<StopStoryboard BeginStoryboardName="BeginScaleTo"/>-->
        </DataTrigger.ExitActions>
    </DataTrigger>

Here is Button

  <ToggleButton Grid.Row="2" VerticalAlignment="Center" HorizontalAlignment="Center" Margin="50,20,50,30" FontSize="20" x:Name="StartButton" Style="{StaticResource StartStopButtonStyle}" Width="100" Height="50" Command="{Binding StartAnimation}">
    </ToggleButton>

And Command to start

  private bool _StartAnim = false;
    public bool RunAnimation
    {
        get => _StartAnim;
        set
        {
            _StartAnim = value;
            RaisePropertyChanged(nameof(RunAnimation));
        }
    }
    public RelayCommand StartAnimation { get; private set; }
    public MainViewModel()
    {
        StartAnimation = new RelayCommand(() => RunAnimation = !RunAnimation);

    }

Solution

  • Something like this should work:

    <Window.Resources>
        <Storyboard x:Key="storyboard2">
            <DoubleAnimation Storyboard.TargetProperty="RenderTransform.ScaleX"
                To="4" From="1" AutoReverse="True"
                RepeatBehavior="Forever" Duration="00:00:10"/>
            <DoubleAnimation  Storyboard.TargetProperty="RenderTransform.ScaleY"
                From="4" To="1" AutoReverse="True"
                RepeatBehavior="Forever" Duration="00:00:10"/>
        </Storyboard>
    </Window.Resources>
    
    <Rectangle Width="100" Height="100" Fill="Red">
        <Rectangle.Style>
            <Style TargetType="Rectangle">
                <Setter Property="RenderTransform">
                    <Setter.Value>
                        <ScaleTransform/>
                    </Setter.Value>
                </Setter>
                <Style.Triggers>
                    <DataTrigger Binding="{Binding RunAnimation}" Value="True">
                        <DataTrigger.EnterActions>
                            <BeginStoryboard x:Name="BeginScaleFrom"
                                Storyboard="{StaticResource storyboard2}"/>
                        </DataTrigger.EnterActions>
                        <DataTrigger.ExitActions>
                            <StopStoryboard BeginStoryboardName="BeginScaleFrom"/>
                        </DataTrigger.ExitActions>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </Rectangle.Style>
    </Rectangle>