5

My WPF application includes a resource file MyResources.resx, containing several strings and images. Because the application will need to be localized, all my references to globalized resources must be made via named properties of the auto-generated MyResources class. The following code works well for string resources:

<Button Content="{x:Static local:Properties.MyResources.ButtonText}" />

However the same does not work for images. Assuming I have an image eflag.bmp added to the resources as a resource named Flag, I would like to be able to do something like this:

<Image Source="{x:Static local:Properties.MyResources.Flag}" />

Please note that the following alternative approach:

<Image Source="/MyNamespace;component/Resources/eflag.bmp" />

cannot be used in this case because it will not be able to handle localization. The problem can be solved using code behind but I am looking for a XAML based solution.

H.B.
  • 166,899
  • 29
  • 327
  • 400
Radu M.
  • 1,271
  • 2
  • 14
  • 19
  • try dragging your image file onto the disign surface and see what source will IDE generate. –  Nov 09 '11 at 09:22
  • how can one drag an image from a resx file? – Radu M. Nov 09 '11 at 10:07
  • What's wring with adding an image to the project like any other file and set its BuildAction to Resource? That way you'll be able to use /MyNamespace** sources, and that's the way most people will do it. do you own that image resources? –  Nov 09 '11 at 10:15
  • and how can I then localize my application? – Radu M. Nov 09 '11 at 10:40
  • by using Satelite Resource Assemblise, see for more details:msdn.microsoft.com/en-us/library/ms788718.aspx –  Nov 09 '11 at 10:50
  • This is ridiculous. If you do not know the answer to the question asked, please allow other people to respond, do not spam this forum. – Radu M. Nov 09 '11 at 10:56
  • What's ridiculous is your anger, my friend. Read on MSDN, that'll help you to avoid silly questions in the future:) And yes, WPF is NOT winforms. –  Nov 09 '11 at 11:07

1 Answers1

8

Turn your x:Static into a Binding.Source and add a Converter which does Bitmap to ImageSource.

Source="{Binding Source={x:Static local:Properties.MyResources.Flag},
                 Converter={StaticResource BitmapToImageSourceConverter}}"

Alternatively you can make the converter a custom markup extension which takes a Bitmap and returns the ImageSource in ProvideValue.

Source="{me:BitmapToImageSource {x:Static local:Properties.MyResources.Flag}}"
Community
  • 1
  • 1
H.B.
  • 166,899
  • 29
  • 327
  • 400