将图像添加到Grid C# [英] Adding Image to Grid C#

查看:192
本文介绍了将图像添加到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 :)

代码:

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

推荐答案

您的代码中没有地方调用CreateImage(),因此:

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.

这篇关于将图像添加到Grid C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆