'WPF Animation Position Parameter

I'm currently making a visual (WPF) representation for the chess engine I wrote. For reference, I used this thread (Mark Feldman's answer) to create the chess board and the chess pieces. Only difference is, that I didn't use the MVVM Light package and instead just used regular MVVM pattern.

Now, as I said, I wrote a chess engine. Whenever I click on a button a random legal move from my engine gets chosen and the pieces on the board get moved around accordingly. This happens by setting the "Pos" property of the instance of the ChessPiece class and due to the INotifyPropertyChanged this gets propagated to the view. But the pieces don't 'move' (obviously), but rather teleport to their destination.

And this is, where I'm stuck right now:

  • I can't define the animation in Xaml code, because the destination is not hard coded, but depends on the move made
  • I also can't define an animation via C# code, because all the images are in the ItemsSource of the ItemsControl and I can't get a reference of the image object. Otherwise I could use this example to create the animation on the fly.

If possible, I'd like to setup the animation via Xaml code to maintain the "data driven"-ness. But if that's not possible, then I'd take the second option.

If you need any further information/details/code, just say it, I'll update the question.

Thank you in advance! Julius



Solution 1:[1]

Maybe I didn't make clear enough, what my problem is: I need a way to create an individual animation to a specific Image object, that is stored inside an ItemsControl. The thing is, that I usually would define the animation in xaml

<BeginStoryboard>
  <Storyboard>
    <DoubleAnimation From="0" To="100" Duration="0:0:2" Storyboard.TargetProperty="(Canvas.Left)" AutoReverse="False" />
  </Storyboard>
</BeginStoryboard>

But I can't define "From" and "To" like

From="{Previous.X}" To="{Next.X}"

So I thought 'Okay, no problem, I'm just gonna do it in C# code'. But the ItemsSource consists of "ObservableCollection", so I have no chance to actually reach the corresponding UIElement (the Image object). Or at least I don't know any.

So maybe I should phrase my question something like this: How can I animate a specific ItemsControl item to a position, that isn't predetermined, but rather comes during execution?

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 PanCave