1

I'm trying to bind my DoubleAnimation properties to c# variables in the code-behind.

This is my XAML:

<Window.Triggers>
    <EventTrigger RoutedEvent="Window.Loaded" >
        <EventTrigger.Actions>
            <BeginStoryboard>
                <Storyboard TargetProperty="Left "  Name ="PlayAnimation">
                    <DoubleAnimation  From="{Binding StartAmount}" To="{Binding EndAmount}" Duration="0:0:0.050"
                                 AutoReverse="True" RepeatBehavior="Forever"
                                 FillBehavior="Stop"/>
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger.Actions>
    </EventTrigger>
</Window.Triggers>

In the MainWindow

public MainWindow()
{         
    InitializeComponent();     
    int StartAmount = 50;
    int EndAmount = 100;      
}

After launching the program I just getting the exception

Cannot animate the 'Left' property on a 'WpfApplication4.MainWindow' using a 'System.Windows.Media.Animation.DoubleAnimation'. For details see the inner exception.

Does anyone has an idea why it is throwing this exception?

Breeze
  • 2,010
  • 2
  • 32
  • 43
Slashy
  • 1,841
  • 3
  • 23
  • 42
  • Not easy but look here: http://stackoverflow.com/questions/2186933/wpf-animation-binding-to-the-to-attribute-of-storyboard-animation – Fruchtzwerg Sep 02 '15 at 18:30

1 Answers1

1

Create a class with two property like this:

public class MyClass
{
    public int StartAmount { get; set; }
    public int EndAmount  { get; set; }
}

Then:

public MainWindow()
{
     InitializeComponent();
     MyClass ms = new MyClass { StartAmount = 50, EndAmount = 100 };
     this.DataContext = ms;
}
Salah Akbari
  • 39,330
  • 10
  • 79
  • 109