I have a stack panel in my XAML page.

 <StackPanel Name="xamlimg" Orientation="Vertical" Grid.column="1">
 </StackPanel>

I want to add an image in this stackpanel using C# code behind the XAML page.

I have attempted using the structure below, but I can't seem to set the source right.

Image img = new Image();
img.Source= //Image Source here
xamlimg.Children.Add(img);

How do I set the image source and add it to the stackpanel?

有帮助吗?

解决方案

You can assign image source using below code:

image.Source = new BitmapImage(new Uri("/MyNameSpace;images/someimage.png", UriKind.Relative));

Thanks

其他提示

it can be many causes, like your img source is inaccurate, the stackpanel is invisible, or it's (parent) size is collaped.

try hand code the img into the xaml, if it worked, then likely u need to look into the container/stackpanel, check it's property about visibility, size, etc.

if it doesn't work, look into the img tag, probably its the source.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top