Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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.

share|improve this question

1 Answer 1

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))
        Board.Children.Add(element);
    Grid.SetColumn(element, x);
    Grid.SetRow(element, y);
}

You could also use a UniformGrid.

A completely data-driven answer can be found in StackOverflow: WPF controls needed to build chess application

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.