Adding Image to Grid C#

2019-09-05 03:49发布

My problem is that the image that I am setting to my grid is not appearing, the only thing appearing is the black background, so I know the grid is working. I am a noob, and I am very confused. Thanks for the Help :)

Code:

    public partial class MainWindow : Window
    {
        static String ImgNameMole = "C:/Users/MonAmi/Desktop/mole2.png";

        public MainWindow()
        {
            InitializeComponent();
            GridMain();
        }

        private void GridMain()
        {
            Grid grid_Main = new Grid();
            MainWindow1.Content = grid_Main;
            grid_Main.Height = 350;
            grid_Main.Width = 525;

            grid_Main.Background = Brushes.GreenYellow;

            CreateImage();

        }

        private Image CreateImage()
        {
            Image Mole = new Image();
            Mole.Width = 25;
            Mole.Height = 25;
            ImageSource MoleImage = new BitmapImage(new Uri(ImgNameMole));
            Mole.Source = MoleImage;
            return Mole;
        }
    }

标签: c# wpf image grid
1条回答
Anthone
2楼-- · 2019-09-05 04:25

Nowhere in your code you are calling CreateImage(), so:

var img = CreateImage();
Grid.SetRow(img, 0);
Grid.SetColumn(img, 0);
grid_Main.Children.Add(img);

assuming that you have added at least one row and one column to your grid.

查看更多
登录 后发表回答