简体   繁体   中英

WPF C# Cannot get image to show via source binding

I cannot get an image to show on my window.

I want to bind the source, but the problem appears to run deeper.

I have these 2 lines:

<Image Grid.Column="1" Grid.Row="1" Source="{Binding EnemyImagePath}" />
<TextBlock Text="{Binding EnemyImagePath}" Grid.Column="0" Grid.Row="1" />

The textbox is showing exactly what I would expect it to. (/RSClick;component/Images/Enemies/001.png). However, image isnt showing at all.

I went ahead and changed the image in XAML to this:

<Image Grid.Column="1" Grid.Row="1" Source="Images/Enemies/001.png" />

and as soon as I did, the image shows up. (I selected the source from the properties panel to get this). I have set the image to Embedded resource and copy always - And I have double checked that it is being created, with the correct folder structure, in the BIN folder for my project.

When I run the program, with the image set statically as above, the image wont show when the program runs. It will, however, show itself normally within the designer...

Thanks :)

The path you provide is called a Pack URI . When using Pack URIs, you have to set the Build Action as Resource .

You can try this

    <Image Source="{Binding ImgPath}" />

public string ImgPath
{
    get { return "pack://application:,,,/AssemblyName;component/Images/image_to_use.png"; }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM