簡體   English   中英

在網格的行和列中添加圖像

[英]Adding Images in Row and Column in Grid

在此處輸入圖片說明

我在嘗試將圖像添加到網格時遇到問題,但是圖像被奇怪地添加了。 (參見圖片)。 我試圖將圖像連續添加,但是將它們添加到第一個位置,然后再添加到下一行。 我是菜鳥,所以很困惑。 謝謝您的幫助。 (TUtils只是從.ini文件中獲取值)

碼:

private void PopulateGrid()
{
    Image img = CreateImage();
    ImageSize = TUtils.GetIniInt(Moleini, "ImageSize", "imageSize", 10);
    NumofImages= TUtils.GetIniInt(Moleini, "NumPictures", "pictures", 8);
    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.");
        MainWin.Close();
    }

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

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

    // Create Grid \\
    MainWin.Content = grid_Main;
    grid_Main.Height = MainWindowHeight;
    grid_Main.Width = MainWindowWidth;
    grid_Main.Background = Brushes.Transparent;

    // Grid Properties \\
    for (int i = 0; i < NumberOfColumns; 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++)
    {
        Image newImage = CreateImage();
        if (RowCount < NumberofRows)
        {
            if (ColumnCount < NumberOfColumns)
            {
                Console.WriteLine("ColumnCount: " + ColumnCount.ToString());
                Grid.SetRow(newImage, ColumnCount);
                Grid.SetColumn(newImage, ColumnCount);
                grid_Main.Children.Add(newImage);
                ColumnCount++;
            }

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

        else
        {
            break;
        }


    }
}

發現問題,當我應該使用“ RowCount”時,我使用了“ ColumnCount”

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM