Click here to Skip to main content
12,622,517 members (31,288 online)
Click here to Skip to main content
Add your own
alternative version


57 bookmarked

WPF: CountryFlag Control

, 30 Aug 2016 CPOL
Rate this:
Please Sign up or sign in to vote.
A WPF user control for displaying a country's flag


The CountryFlag user control, as the name suggests, can be used to diplay the flag of a country in a WPF application. A flag is specified using a country's ISO 3166-1 alpha-2 country code.


Your project's target framework should be .NET Framework 4.6 or higher.


To use the CountryFlag control first install it by running the following command in the NuGet Package Manager Console,

PM> Install-Package CountryFlag

You can also install it by using the NuGet Package Manager. In Solution Explorer right-click your project, select Manage NuGet Packages, and from the Browse tab search for "countyflag" and install.

When installation is complete you can now add a XAML reference and add the control/s to a layout control. To specify a flag set the control's Code property.

<Window x:Class="Flags.MainWindow"
            <cf:CountryFlag Code="BT" Margin="5"/>
            <cf:CountryFlag Code="AF" Margin="5"/>
            <cf:CountryFlag Code="AO" Margin="5"/>
            <cf:CountryFlag Code="BB" Margin="5"/>
            <cf:CountryFlag Code="KE" Margin="5"/>
            <cf:CountryFlag Code="BR" Margin="5"/>
            <cf:CountryFlag Code="EG" Margin="5"/>
            <cf:CountryFlag Code="RS" Margin="5"/>
            <cf:CountryFlag Code="SZ" Margin="5"/>

The above code declares nine flag controls with their Code property set to the ISO 3166 code of a specific country. The Code property is set to a value of type CountryCode, an enumeration of ISO 3166 country codes specified in the control library. The following image shows the result of the above markup, in the artboard of the XAML Designer,


The code behind for the user control contains a single dependency property and a callback method,

Class CountryFlag
    Public Property Code() As CountryCode
            Return CType(GetValue(CodeProperty), CountryCode)
        End Get
        Set(ByVal value As CountryCode)
            SetValue(CodeProperty, value)
        End Set
    End Property

    Public Shared CodeProperty As DependencyProperty =
        DependencyProperty.Register("Code", GetType(CountryCode), GetType(CountryFlag),
                                    New PropertyMetadata(CountryCode.AD,
                                                         New PropertyChangedCallback(AddressOf ChangeFlag)))

    Private Shared Sub ChangeFlag(ByVal source As DependencyObject, ByVal e As DependencyPropertyChangedEventArgs)
        Dim cCode = CType(e.NewValue, CountryCode).ToString()
        Dim flag = "Flags/" & cCode.ToLower() & ".png"
        CType(source, CountryFlag).Flag.Source = New BitmapImage(New Uri(flag, UriKind.Relative))
    End Sub
End Class

In the callback method the value of the property is used to set the source property of an Image control.


You can download the sample project from the link at the top of the article page. The project contains the sample code highlighted earlier.


  • 3rd May 2011: Initial post
  • 30th August 2016: Updated code and flags


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 has about 7 years of programming experience initially starting off with Java before shifting to .NET, thanks to the allure of WPF.

  • 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

SuggestionSource code? Pin
Richard Deeming31-Aug-16 6:55
mvpRichard Deeming31-Aug-16 6:55 
GeneralRe: Source code? Pin
Meshack Musundi31-Aug-16 8:44
professionalMeshack Musundi31-Aug-16 8:44 
GeneralMy vote of 5 Pin
fredatcodeproject15-May-13 2:24
memberfredatcodeproject15-May-13 2:24 
GeneralRe: My vote of 5 Pin
Meshack Musundi16-May-13 3:57
mvpMeshack Musundi16-May-13 3:57 
GeneralMy vote of 5 Pin
GeekBond 31-Oct-12 5:47
member GeekBond 31-Oct-12 5:47 
GeneralRe: My vote of 5 Pin
Meshack Musundi31-Oct-12 7:52
mvpMeshack Musundi31-Oct-12 7:52 
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 
GeneralMy vote of 5 Pin
IFFI11-Jul-11 22:36
memberIFFI11-Jul-11 22:36 
GeneralRe: My vote of 5 Pin
Meshack Musundi11-Jul-11 23:30
memberMeshack Musundi11-Jul-11 23:30 
GeneralRe: My vote of 5 Pin
BeeWayDev9-Dec-11 6:37
memberBeeWayDev9-Dec-11 6:37 
GeneralRe: My vote of 5 Pin
Meshack Musundi9-Dec-11 21:17
memberMeshack Musundi9-Dec-11 21:17 
I mostly design my UIs using Expression Design, then export the design as a WPF canvas. One of the first tutorials I read when I got started was the Expression Design Quick Start Guide, which you can find in the 'Using Expression Design 1' section here.

You can also look around for some Expression Blend tutorials. I started off reading the ones on Kirupa's website which you can access here.

After that I just played around with both Expression Design and Blend. Experimentation is a good way to get a good grasp of things. Just use your imagination.
GeneralRe: My vote of 5 Pin
BeeWayDev15-Dec-11 5:04
memberBeeWayDev15-Dec-11 5:04 
GeneralMy vote of 4 Pin
germ1311-Jul-11 21:30
membergerm1311-Jul-11 21:30 
GeneralRe: My vote of 4 Pin
Meshack Musundi11-Jul-11 23:29
memberMeshack Musundi11-Jul-11 23:29 
GeneralMy vote of 5 Pin
Md. Marufuzzaman15-Jun-11 21:01
mvpMd. Marufuzzaman15-Jun-11 21:01 
GeneralRe: My vote of 5 Pin
Meshack Musundi16-Jun-11 6:04
memberMeshack Musundi16-Jun-11 6:04 
GeneralRe: My vote of 5 Pin
Md. Marufuzzaman16-Jun-11 7:14
mvpMd. Marufuzzaman16-Jun-11 7:14 
GeneralNeat, but ... Pin
Mr. Mox10-May-11 4:04
memberMr. Mox10-May-11 4:04 
GeneralRe: Neat, but ... Pin
Meshack Musundi10-May-11 6:39
memberMeshack Musundi10-May-11 6:39 
GeneralRe: Neat, but ... Pin
Jaime Olivares11-Jul-11 11:54
memberJaime Olivares11-Jul-11 11:54 
GeneralRe: Neat, but ... Pin
Meshack Musundi11-Jul-11 23:28
memberMeshack Musundi11-Jul-11 23:28 
GeneralRe: Neat, but ... Pin
Florian Rappl20-Jan-14 9:26
mvpFlorian Rappl20-Jan-14 9:26 
GeneralRe: Neat, but ... Pin
Meshack Musundi20-Jan-14 19:35
professionalMeshack Musundi20-Jan-14 19:35 
SuggestionRe: Neat, but ... Pin
germ1311-Jul-11 21:29
membergerm1311-Jul-11 21:29 

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
Web01 | 2.8.161128.1 | Last Updated 31 Aug 2016
Article Copyright 2011 by Meshack Musundi
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid