Click here to Skip to main content
15,879,613 members
Articles / Desktop Programming / WPF

WPF: CountryFlag Control

Rate me:
Please Sign up or sign in to vote.
4.87/5 (46 votes)
29 Aug 2017CPOL1 min read 78.9K   2.5K   60   30
A WPF user control for displaying a country's flag

Image 1

Introduction

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.

Requirements

  •  .NET Framework 4.6

Usage

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.

Image 2

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.

XML
<Window x:Class="Flags.MainWindow"
        ...
        xmlns:cf="clr-namespace:CountryFlag;assembly=CountryFlag"
        ...>
    ...
    <Grid>
        <WrapPanel>
            <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"/>
        </WrapPanel>
    </Grid>
</Window>

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,

Image 3

CountryFlag

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

VB.NET
Class CountryFlag
    Public Property Code() As CountryCode
        Get
            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.

Conclusion

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

History

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

License

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


Written By
Software Developer
Kenya Kenya
Experienced C# software developer with a passion for WPF.

Awards,
  • CodeProject MVP 2013
  • CodeProject MVP 2012
  • CodeProject MVP 2021

Comments and Discussions

 
SuggestionSource code? Pin
Richard Deeming31-Aug-16 5:55
mveRichard Deeming31-Aug-16 5:55 
GeneralRe: Source code? Pin
Meshack Musundi31-Aug-16 7:44
professionalMeshack Musundi31-Aug-16 7:44 
GeneralMy vote of 5 Pin
fredatcodeproject15-May-13 1:24
professionalfredatcodeproject15-May-13 1:24 
GeneralRe: My vote of 5 Pin
Meshack Musundi16-May-13 2:57
professionalMeshack Musundi16-May-13 2:57 
GeneralMy vote of 5 Pin
Akinmade Bond31-Oct-12 4:47
professionalAkinmade Bond31-Oct-12 4:47 
GeneralRe: My vote of 5 Pin
Meshack Musundi31-Oct-12 6:52
professionalMeshack Musundi31-Oct-12 6:52 
GeneralMy vote of 5 Pin
BeeWayDev9-Dec-11 5:34
BeeWayDev9-Dec-11 5:34 
GeneralRe: My vote of 5 Pin
Meshack Musundi12-Jan-12 4:54
professionalMeshack Musundi12-Jan-12 4:54 
GeneralMy vote of 5 Pin
Iftikhar Akram11-Jul-11 21:36
Iftikhar Akram11-Jul-11 21:36 
GeneralRe: My vote of 5 Pin
Meshack Musundi11-Jul-11 22:30
professionalMeshack Musundi11-Jul-11 22:30 
GeneralRe: My vote of 5 Pin
BeeWayDev9-Dec-11 5:37
BeeWayDev9-Dec-11 5:37 
GeneralRe: My vote of 5 Pin
Meshack Musundi9-Dec-11 20:17
professionalMeshack Musundi9-Dec-11 20:17 
GeneralRe: My vote of 5 Pin
BeeWayDev15-Dec-11 4:04
BeeWayDev15-Dec-11 4:04 
GeneralMy vote of 4 Pin
germ1311-Jul-11 20:30
germ1311-Jul-11 20:30 
GeneralRe: My vote of 4 Pin
Meshack Musundi11-Jul-11 22:29
professionalMeshack Musundi11-Jul-11 22:29 
GeneralMy vote of 5 Pin
Md. Marufuzzaman15-Jun-11 20:01
professionalMd. Marufuzzaman15-Jun-11 20:01 
GeneralRe: My vote of 5 Pin
Meshack Musundi16-Jun-11 5:04
professionalMeshack Musundi16-Jun-11 5:04 
GeneralRe: My vote of 5 Pin
Md. Marufuzzaman16-Jun-11 6:14
professionalMd. Marufuzzaman16-Jun-11 6:14 
GeneralNeat, but ... Pin
Gustav Brock10-May-11 3:04
professionalGustav Brock10-May-11 3:04 
GeneralRe: Neat, but ... Pin
Meshack Musundi10-May-11 5:39
professionalMeshack Musundi10-May-11 5:39 
GeneralRe: Neat, but ... Pin
Jaime Olivares11-Jul-11 10:54
Jaime Olivares11-Jul-11 10:54 
GeneralRe: Neat, but ... Pin
Meshack Musundi11-Jul-11 22:28
professionalMeshack Musundi11-Jul-11 22:28 
GeneralRe: Neat, but ... Pin
Florian Rappl20-Jan-14 8:26
professionalFlorian Rappl20-Jan-14 8:26 
GeneralRe: Neat, but ... Pin
Meshack Musundi20-Jan-14 18:35
professionalMeshack Musundi20-Jan-14 18:35 
SuggestionRe: Neat, but ... Pin
germ1311-Jul-11 20:29
germ1311-Jul-11 20: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.