Andrew
Andrew

Reputation: 545

Adding Image to Grid C#

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;
        }
    }

Upvotes: 1

Views: 16718

Answers (1)

gzaxx
gzaxx

Reputation: 17600

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.

Upvotes: 6

Related Questions