Question

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?

Was it helpful?

Solution

You can assign image source using below code:

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

Thanks

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top