网格到数组[] []或在每个单元格中查找项目

时间:2015-05-31 23:13:39

标签: c# windows-phone-7

我有一个包含9张图像的网格3x3。是否可以将其转换为Images array [] [],或者只是循环遍历网格的每个单元格?

关键在于我的每个9个单元格中都有一个图像。我想要的只是知道哪个图像在特定的单元格中。

修改

这是我的XAML代码,但它只是在我的布局上创建单元格

<Grid x:Name="Map">
        <!-- Row and coulmns definition -->
        <Grid.RowDefinitions>
            <RowDefinition Height="120"/>
            <RowDefinition Height="120"/>
            <RowDefinition Height="120"/>
            <RowDefinition Height="120"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="200"/>
            <ColumnDefinition Width="200"/>
            <ColumnDefinition Width="200"/>
            <ColumnDefinition Width="0"/>
            <ColumnDefinition Width="128*" />
        </Grid.ColumnDefinitions>

        <!-- Canvas & Components  -->

        <Canvas
        x:Name="MyCanvas"
        Background="Azure"
        Grid.ColumnSpan="3"
        Grid.RowSpan="3">
        </Canvas>
        <Button Background="Chocolate" Content="Spin" Name="SpinButton" Grid.Row="3" Grid.Column="3" Click="SpinButton_Click" Grid.ColumnSpan="2" Margin="15,36,12,12" />
    </Grid>

我主要用C#代码做各种事情。 我使用Storyboard类动画图像,最后将我的9个图像设置为特定位置(每个图像分别位于9个单元格中)。 我得到你的代码也尝试将Image更改为FrameworkElement,但无论如何它只是看到Canvas Element。也许有一个选项可以从特定坐标中获取元素?

添加图片C#:

 public List<Image> SetImages(String order)
        {
            List<Image> line = new List<Image>();

            for (int i = 0; i < imagesQty; i++)
            {
                Image img1 = new Image();
                img1.Source = ananasImage;
                img1.Stretch = Stretch.Fill;
                img1.Width = 200;
                img1.Height = 120;
                if (order.Equals("left"))
                {
                    line.Add(img1);
                    Canvas.SetTop(img1, -200);
                    Canvas.SetLeft(img1, 0);
                    MyCanvas.Children.Add(img1);
                }
                else if (order.Equals("middle"))
                {
                    line.Add(img1);
                    Canvas.SetTop(img1, -200);
                    Canvas.SetLeft(img1, 180);
                    MyCanvas.Children.Add(img1);
                }

                else if (order.Equals("right"))
                {
                    line.Add(img1);
                    Canvas.SetTop(img1, -200);
                    Canvas.SetLeft(img1, 360);
                    MyCanvas.Children.Add(img1);
                }

            }

            return line;
        }

1 个答案:

答案 0 :(得分:0)

您可以使用Children的{​​{1}}属性枚举子控件。此外,您可以使用GridGrid.GetRow方法来了解控件的行/列。从那里开始,这只是将这些比特放在一起的问题:

Grid.GetColumn

此外,如果您想知道图像在哪一行/列,您需要首先将它们添加到网格中:

int row = 3;
int column = 2;

var image = grid.Children.OfType<Image>().FirstOrDefault(i => Grid.GetRow(i) == row && Grid.GetColumn(i) == column);

if (image != null)
{
    // Found the picture on line 3 and column 2
}