Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Images bound to images added to resx files using XAML

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.

like image 997
Radu M. Avatar asked Nov 09 '11 08:11

Radu M.


1 Answers

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}}"
like image 113
H.B. Avatar answered Sep 30 '22 20:09

H.B.