.NET logo

country flag image value converter

home | software | wpf | country flag image value converter

Recently, I've been working on a WPF application that displays information about trades in various equities and other types of securities that are traded around the world. It struck me in the shower one morning (I have some of my best ideas there) that the good people at FamFamFam have a catalog of flag images which are publicly available, and that I could use these flags in my application to indicate which country the security's exchange is located in.

Thanks to the greatness of WPF, this was simple and fun to do. I've made the library I created available here for others to use.

using the code

The following code creates an image of flag that corresonds to the two letter ISO country code that is resolved by the binding expression. Note that you may have to include a path in your binding, depending upon the structure of your DataContext.

<Image Source="{Binding Converter={StaticResource CountryIdToFlagImageSourceConverter}}"/>

The converter in this binding transforms the country code into an ImageSource. This requires an instance of the converter class to be within scope as a resource. You might use code such as this somewhere up the logical tree:

<Flags:CountryIdToFlagImageSourceConverter x:Key="CountryIdToFlagImageSourceConverter" />

You will also have to declare the Flags XML namespace by including this code as an attribute on any ancestor node of the above declaration.



See the converter running in this live XBAP demo (requires Silverlight plugin).


submitting feedback

Please feel free to provide feedback on this class .

Authored by Drew Noakes, May 2009. Use freely, though keep this message in the source intact and report any bugs to me. I also appreciate seeing extensions, or simply hearing that you're using these classes. You may not copyright this work, though may use it in commercial/copyrighted works. Happy coding.

Flag images by FamFamFam.