Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WPF-将图像设置为网格_C#_Wpf_Grid - Fatal编程技术网

C# WPF-将图像设置为网格

C# WPF-将图像设置为网格,c#,wpf,grid,C#,Wpf,Grid,我正在尝试将图像设置为我创建的网格,我是一个noob,所以请不要对我的编码生气:)。我想知道是否有一种方法可以说设置图像到网格行1,网格行2,等等。我想做一个打鼹鼠的游戏 private void PopulateGrid() { double NumofImages = TUtils.GetIniInt(Moleini, "NumPictures", "pictures", 8); int ImageSize = TUtils.GetIniInt(Moleini, "Image

我正在尝试将图像设置为我创建的网格,我是一个noob,所以请不要对我的编码生气:)。我想知道是否有一种方法可以说设置图像到网格行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;
        }

    }
}

我的MVVM示例怎么了?@HighCore嘿,我是用你的方式做的,但我的老板坚持我必须按照我已经开始的方式做,顺便说一句,你的示例工作起来很好,看起来很神奇。是不是你的老板不理解我的示例?WPF需要MVVM,否则就是一堆可怕的不需要的代码。我同意,WPF需要MVVM。如果您想使用winforms样式,只需使用
UniformGrid
而不是
Grid
,设置行/列的数量,然后添加图像即可
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;


}