WPF Binding Source from Project Resources

Well, I have about 5 Images in my Project Resources. I want to do to bind Image.Source to my project resources. Im C # code is pretty easy, I just do:

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

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

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

Thanks in advance.

+4
source share
2 answers

Visual studio will create the Resources folder and place the image file in it when you add the image to the resx file.

To use this image in a binding, you will need to change the build action from None to Resource. After that, you can communicate as follows:

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

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

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

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

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

+5
source

Make sure your Build Action for image marked as Resource , and then you can just do it in your XAML -

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

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

+2
source

Source: https://habr.com/ru/post/1495175/


All Articles