Click here to Skip to main content
12,074,995 members (57,990 online)
Click here to Skip to main content
Add your own
alternative version


50 bookmarked

WPF: CountryFlag Control

, 4 May 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
A WPF UserControl for displaying the flag of a particular country

Image: Several CountryFlag controls in action


The CountryFlag control is a UserControl that allows you to display the flag of a particular country, in your WPF application, from a choice of 223 countries. This article describes how to use the control and how I went about creating it. The control will hopefully save you from having to scour the net for flag images and from writing numerous lines of code to implement the functionality it provides.


In order to use the control, add a reference to CountryFlag.dll in your project. In Expression Blend, make the Assets panel active and start typing the word 'CountryFlag' in the Search box. The CountryFlag control should be displayed as you continue typing.


Double-click on the control to add it to your application or select it and draw out the control in your application window.


With the control you've just added still selected, look for the Flag property in the Miscellaneous section of the Properties panel.


Expand the combobox and select the country whose flag you want to display.


Once you select a country, its flag should be displayed in the control.


Specifying a Flag in Code

You can also specify which flag you want to display using code.


Imports CountryFlag

Class MainWindow
    Private Function ShowFlag() As String
        CountryFlagCtrl.Flag = Country.Kenya

        Dim countryFlg As String = CountryFlagCtrl.Flag.ToString
        Return countryFlg
    End Function
End Class


private string ShowFlag()
    CountryFlagCtrl.Flag = Country.Kenya;

    string countryFlg = CountryFlagCtrl.Flag.ToString();
    return countryFlg;

Once you type the equal sign IntelliSense should help you along:


Design and Layout

The control contains a single Layout control and an Image control named FlagImage:


The Code

The CountryFlag UserControl library contains an enumeration, Country, that contains the names of 223 countries. Here's parts of the enum:

Public Enum Country
End Enum

The UserControl class contains code for creating a DependencyProperty of type Country:

Class CountryFlag
    Public Property Flag() As Country
            Return CType(GetValue(FlagProperty), Country)
        End Get
        Set(ByVal value As Country)
            SetValue(FlagProperty, value)
        End Set
    End Property

    Public Shared FlagProperty As DependencyProperty = _
        DependencyProperty.Register("Flag", _
                                    GetType(Country), _
                                    GetType(CountryFlag), _
                                    New FrameworkPropertyMetadata( _
                                    New PropertyChangedCallback(AddressOf ChangeFlag)))

    Private Shared Sub ChangeFlag(ByVal source As DependencyObject, _
                                  ByVal e As DependencyPropertyChangedEventArgs)

        Dim countryName As String = CType(e.NewValue, Country).ToString
        Dim path As String = "flags/" & countryname & ".png"

        CType(source, CountryFlag).FlagImage.Source = _
            New BitmapImage(New Uri(path, UriKind.Relative))
    End Sub
End Class

Notice that in the callback method, ChangeFlag, we specify the image for the control's Image control. The reason the .dll does not need to come bundled with a separate folder containing image files is because the images are embedded in the assembly. If you look in the Solution Explorer of Visual Studio, you will notice a folder named Flags that contains the image files.



CountryFlag will hopefully prove to be useful and I hope you gained more than that from reading the article. Cheers!


  • 3rd May, 2011: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Meshack Musundi
Software Developer
Kenya Kenya
Meshack is an avid programmer with a bias towards WPF and VB.NET. He has about 5 years of programming experience initially starting off with Java before shifting to .NET, thanks to the allure of WPF. He currently resides in a small town in Kiambu county, Kenya.

  • CodeProject MVP 2013
  • CodeProject MVP 2012
  • Best VB.NET article of January 2015
  • Best VB.NET article of August 2013
  • Best VB.NET article of February 2013
  • Best VB.NET article of October 2012
  • Best VB.NET article of July 2012
  • Best VB.NET article of February 2012
  • Best VB.NET article of January 2012
  • Best VB.NET article of November 2011
  • Best VB.NET article of June 2011
  • Best VB.NET article of May 2011
  • Best VB.NET article of March 2011
  • Best VB.NET article of February 2011
  • Best VB.NET article of January 2011
  • Best VB.NET article of December 2010
  • Best VB.NET article of November 2010

You may also be interested in...

Comments and Discussions

GeneralMy vote of 5 Pin
BeeWayDev9-Dec-11 6:34
memberBeeWayDev9-Dec-11 6:34 
GeneralRe: My vote of 5 Pin
Meshack Musundi12-Jan-12 5:54
mvpMeshack Musundi12-Jan-12 5:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160208.1 | Last Updated 5 May 2011
Article Copyright 2011 by Meshack Musundi
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid