wpf 将图像添加到网格 C#
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16742262/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adding Image to Grid C#
提问by Andrew
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;
}
}
回答by gzaxx
Nowhere in your code you are calling CreateImage(), so:
在您调用的代码中没有任何地方CreateImage(),因此:
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.
假设您已向网格中添加了至少一行和一列。

