Adding Image to Grid C#

╄→гoц情女王★ 提交于 2019-12-14 03:18:43

问题


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

回答1:


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.



来源:https://stackoverflow.com/questions/16742262/adding-image-to-grid-c-sharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!