-1

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

PanCave
  • 21
  • 8
  • Does this answer your question? [Starting an animation from the ViewModel in WPF/MVVM](https://stackoverflow.com/questions/2531570/starting-an-animation-from-the-viewmodel-in-wpf-mvvm) – eocron Mar 28 '22 at 19:58
  • Correct me, if I'm wrong, but I'd say the answer shows, how to trigger an already defined animation using a DataTrigger. But my problem is defining an animation in the first place. – PanCave Mar 28 '22 at 20:52
  • I don't exactly know what you mean with references to image objects and such. But when the peice is moved I assume it now set a new position for the corresponding piece image immediately? Could you use an IEnumerator instead and move the piece image from its current position to the new position in time steps? – eligolf Mar 29 '22 at 05:05
  • @eligolf That sounds very interesting. Can you show me, how I can get the piece image? I know, I can get the ContentPresenter with this line: ContentPresenter c = (ContentPresenter)this.Chessboard.ItemContainerGenerator.ContainerFromIndex(i); But how can I access the image from there? – PanCave Apr 02 '22 at 10:37
  • I am sorry, I don't know wpf or this particular code. But how do you move the piece now? Or the image? – eligolf Apr 02 '22 at 12:43

1 Answers1

0

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?

PanCave
  • 21
  • 8