【问题标题】:Grid to array[][] or finding items in each cell网格到数组 [] [] 或在每个单元格中查找项目
【发布时间】:2015-06-04 16:02:05
【问题描述】:

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

重点是在我的 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 元素。也许可以选择从特定坐标中获取元素?

添加图像 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;
        }

【问题讨论】:

  • 您能提供一个现在的代码示例吗?
  • 只有行 && 列定义。故事板将图像放入其中。

标签: c# windows-phone-7


【解决方案1】:

您可以使用GridChildren 属性来枚举子控件。此外,您可以使用Grid.GetRowGrid.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
}

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

if (order.Equals("left"))
{
    line.Add(img1);
    Grid.SetColumn(img1, 0);
    Grid.SetLine(img1, 0);
    Map.Children.Add(img1);
}
else if (order.Equals("left"))
{
    line.Add(img1);
    Grid.SetColumn(img1, 1);
    Grid.SetLine(img1, 0);
    Map.Children.Add(img1);
}
// and so on

【讨论】:

  • 图像一直为空;/
  • 你在哪里添加你的图片?
  • 已编辑。我想我明白了我没有将图像添加到网格中。无论如何,我不能将我的图像添加到 Grid,因为它们已经是 Canvas 的孩子。那有什么解决办法呢?;(
  • 为什么不直接将图像添加到网格中?使用Grid.SetRowGrid.SetColumn 来判断它们应该放在哪一行/哪一列
  • 因为不能添加Image,所以只能添加FrameworkElemet。当我尝试添加图像时,“错误 1 ​​'System.Windows.Controls.Grid.SetRow(System.Windows.FrameworkElement, int)' 的最佳重载方法匹配有一些无效参数
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-08-20
  • 2017-06-15
  • 2021-09-10
相关资源
最近更新 更多