0

我正在尝试将图像设置为我创建的网格,我是菜鸟,所以请不要对我的编码生气:)。我想知道是否有办法将图像设置为网格第 1 行、网格第 2 行等。我正在尝试制作一个鼹鼠游戏。

private void PopulateGrid()
{
    double NumofImages = TUtils.GetIniInt(Moleini, "NumPictures", "pictures", 8);
    int ImageSize = TUtils.GetIniInt(Moleini, "ImageSize", "imageSize", 50);
    int ImageBorderSize = TUtils.GetIniInt(Moleini, "ImageBorder", "imageBorder", 2);
    double NumberOfColumns = TUtils.GetIniInt(Moleini, "NumRowsColumns", "columnNum", 4);

    // More Columns than Rows \\
    if (NumberOfColumns > NumofImages)
    {
        MessageBox.Show("There is something wrong with the .ini file.");
        Window1.Close();
    }

    // Math - Get Necessary Variables \\
    int ColumnSize = (ImageSize + (4 * ImageBorderSize));
    int RowSize = (ImageSize + (4 * ImageBorderSize));
    int NumberofRows = (int)Math.Ceiling(NumofImages / NumberOfColumns);
    int MainWindowWidth = (TUtils.ToInt(NumberOfColumns.ToString(), 4) * ColumnSize) + 15;
    int MainWindowHeight = (NumberofRows * RowSize) + 35;

    // Set Window Size \\
    Window1.Width = MainWindowWidth;
    Window1.Height = MainWindowHeight;

    // Create Grid \\
    Window1.Content = grid_Main;
    grid_Main.Height = MainWindowHeight;
    grid_Main.Width = MainWindowWidth;

    // Grid Properties \\
    for (int i = 0; i < NumberofRows; i++)
    {
        ColumnDefinition newColumn = new ColumnDefinition();
        newColumn.Width = new GridLength(ColumnSize, GridUnitType.Pixel);
        grid_Main.ColumnDefinitions.Add(newColumn);
    }

    for (int i = 0; i < NumberofRows; i++)
    {
        RowDefinition Row = new RowDefinition();
        Row.Height = new GridLength(RowSize, GridUnitType.Pixel);
        grid_Main.RowDefinitions.Add(Row);
    }


    // Fill Grid \\
    int RowCount = 0;
    int ColumnCount = 0;

    for (int i = 0; i < NumofImages; i++)
    {
        grid_Main.Children.Add(grid_Main);

        if (RowCount < NumberofRows)
        {
            if (ColumnCount < NumberOfColumns)
            {
                Console.WriteLine("ColumnCount: " + ColumnCount.ToString());
                Grid.SetRow(grid_Main, ColumnCount);
                Grid.SetColumn(grid_Main, ColumnCount);
                ColumnCount++;
            }

            else
            {
                RowCount++;
                ColumnCount = 0;
                Grid.SetRow(grid_Main, ColumnCount);
                Grid.SetColumn(grid_Main, ColumnCount);
                ColumnCount++;
                Console.WriteLine("RowCount: " + RowCount.ToString());
            }
        }

        else
        {
            break;
        }

    }
}

...

private Image CreateImage(int ImageNum)
{
    // Gets/Sets Necessary Variables \\
    double ImageHeight = ImageSize * 0.7;

    // Initialize Image \\
    System.Windows.Controls.Image newImage = new Image();

    // Image Properties \\
    newImage.Width = ImageSize;
    newImage.Height = ImageHeight;

    // Define Name and Content \\
    newImage.Name = "Image_" + ImageNum;
    String ImageFunction = TUtils.GetIniString(Moleini, "Image" + ImageNum, "PictureFile", Root + "mole2.png");
    if (File.Exists(ImageFunction))
    {
        newImage.Source = new BitmapImage(new Uri(ImageFunction));
    }
    else
    {
        MessageBox.Show("Cannot find " + ImageFunction + ".", "Please fix the ini file");
    }

    return newImage;


}
4

0 回答 0