# Getting Image name given a canvas WPF

I have a bunch of images I'm using for a chess game in a canvas using wpf, xaml, and C#. I want to use

Canvas.SetTop(image, position);


to set the position of this image. However I am given the index of child in my canvas and that is it.

myCanvas.children[index]


I tried

Canvas.SetTop(myCanvas.children[index], position);


but that did not work. How can I change the position of the image given only the index of the image in the canvas?

Edit: Another option would be to change the position of the canvas child. But I couldn't find anything for that.

Since you're using WPF I expect you don't plan to use a lot of animation, so a better way to do this would be with a Grid:

<!-- XAML -->
<Grid x:Name="Board">
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
</Grid>


To place something in a cell:

void SetPosition(UIElement element, int x, int y)
{
if (!Board.Children.Contains(element))

myCanvas.Children[index].SetValue(Canvas.TopProperty, position);