Click here to Skip to main content
13,354,886 members (89,112 online)
Click here to Skip to main content
Add your own
alternative version


60 bookmarked
Posted 3 May 2011

WPF: CountryFlag Control

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


CountryFlag is a WPF user control for displaying the flag of any one of the 249 countries and territories assigned with an ISO 3166-1 alpha-2 code.

To get the project code you can clone or download the project from GitHub.


  •  .NET Framework 4.6


Install the CountryFlag NuGet package by running the following command in the NuGet Package Manager Console,

PM> Install-Package CountryFlag -Version 2.1.0

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 an ISO 3166-1 alpha-2 code . The Code property is set to a value of type CountryCode, an enumeration of ISO 3166-1 alpha-2 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 countryCode = CType(e.NewValue, CountryCode).ToString()
        Dim flag = "Flags/" & countryCode & ".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,
  • 30th August 2017: 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 is a software developer with a passion for WPF.


  • CodeProject MVP 2013
  • CodeProject MVP 2012

You may also be interested in...


Comments and Discussions

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 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.180111.1 | Last Updated 29 Aug 2017
Article Copyright 2011 by Meshack Musundi
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid