Radu M.
Radu M.

Reputation: 1391

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.

Upvotes: 5

Views: 5386

Answers (1)

brunnerh
brunnerh

Reputation: 184752

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}}"

Upvotes: 8

Related Questions