Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 341
oimitro Avatar asked Aug 04 '13 10:08

oimitro


2 Answers

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

like image 126
knov Avatar answered Sep 28 '22 08:09

knov


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.

like image 41
Rohit Vats Avatar answered Sep 28 '22 09:09

Rohit Vats