문제

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