oimitro
oimitro

Reputation: 1506

WPF Binding image source from Project Resources

Ok i have in my project Resources about 5 Images. What i want to do is to Bind an Image.Source from my Project Resources. Im C# code its pretty easy, i just do :

ImageHolder.Source = Propetries.Resources.Image1.png.

How can this be done in XAML? Something like this :

<Image Source={??????}/>

Thanks in advance.

Upvotes: 5

Views: 25963

Answers (2)

knov
knov

Reputation: 116

Visual studio will create Resources folder and put your image file into it when you add image to the resx file.

In order to use this image in binding you will need to change build action from None to Resource. After that you can bind as follows:

<Image Source="Resources/your_image_name.png"/>

You can not bind directly to Propetries.Resources.your_image_name because of you will need to convert System.Drawing.Bitmap to WPF BitmapSource. But you can bind to strings in the Resource.resx:

<TextBlock Text="{x:Static properties:Resources.YourStringResource}"></TextBlock>

Read here how to convert System.Darwing.Bitmap to the WPF bitmap: Load a WPF BitmapImage from a System.Drawing.Bitmap

And here about binding to the values in the resx file: Get values from *.resx files in XAML

Upvotes: 6

Rohit Vats
Rohit Vats

Reputation: 81253

Make sure your Build Action for image is marked as Resource and then you can simply do this in your XAML -

<Image Source="Properties/Resources/a.png"/>

Assuming Propetries/Resources is folder structure in your project where your image is present.

Upvotes: 2

Related Questions