Click here to Skip to main content
Click here to Skip to main content

WPF: CountryFlag Control

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

Image: Several CountryFlag controls in action

Introduction

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.

CountryFlag

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.

Screenshot_2.png

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

Screenshot_3.png

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

Screenshot_4.png

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

Screenshot_5.png

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

Screenshot_6.png

Specifying a Flag in Code

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

VB.NET

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

C#

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:

Screenshot_7.png

Design and Layout

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

Screenshot_9.png

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
    Afghanistan
    Albania
    Algeria
    American_Samoa
    Andorra
    Angola
    Anguilla
    Antigua_and_Barbuda
    .
    .
    .
    Venezuela
    Vietnam
    Wallis_and_Futuna
    Yemen
    Zambia
    Zimbabwe
End Enum

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

Class CountryFlag
    Public Property Flag() As Country
        Get
            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.

Screenshot_8.png

Conclusion

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

History

  • 3rd May, 2011: Initial post

License

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

Share

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 has developed several applications, and written several articles about them, which can be viewed here on CodeProject. He currently resides in a small town in Kiambu county, Kenya.
 
Awards;
  • CodeProject MVP 2013
  • CodeProject MVP 2012
  • 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

Comments and Discussions

 
GeneralMy vote of 5 Pinmemberfredatcodeproject15-May-13 1:24 
GeneralRe: My vote of 5 PinmvpMeshack Musundi16-May-13 2:57 
GeneralMy vote of 5 Pinmember GeekBond 31-Oct-12 4:47 
GeneralRe: My vote of 5 PinmvpMeshack Musundi31-Oct-12 6:52 
GeneralMy vote of 5 PinmemberBeeWayDev9-Dec-11 5:34 
GeneralRe: My vote of 5 PinmvpMeshack Musundi12-Jan-12 4:54 
GeneralMy vote of 5 PinmemberIFFI11-Jul-11 21:36 
GeneralRe: My vote of 5 PinmemberMeshack Musundi11-Jul-11 22:30 
GeneralRe: My vote of 5 PinmemberBeeWayDev9-Dec-11 5:37 
GeneralRe: My vote of 5 PinmemberMeshack Musundi9-Dec-11 20:17 
GeneralRe: My vote of 5 PinmemberBeeWayDev15-Dec-11 4:04 
GeneralMy vote of 4 Pinmembergerm1311-Jul-11 20:30 
GeneralRe: My vote of 4 PinmemberMeshack Musundi11-Jul-11 22:29 
GeneralMy vote of 5 PinmvpMd. Marufuzzaman15-Jun-11 20:01 
GeneralRe: My vote of 5 PinmemberMeshack Musundi16-Jun-11 5:04 
GeneralRe: My vote of 5 PinmvpMd. Marufuzzaman16-Jun-11 6:14 
GeneralNeat, but ... PinmemberMr. Mox10-May-11 3:04 
GeneralRe: Neat, but ... PinmemberMeshack Musundi10-May-11 5:39 
GeneralRe: Neat, but ... PinmemberJaime Olivares11-Jul-11 10:54 
GeneralRe: Neat, but ... PinmemberMeshack Musundi11-Jul-11 22:28 
GeneralRe: Neat, but ... PinmvpFlorian Rappl20-Jan-14 8:26 
GeneralRe: Neat, but ... PinprofessionalMeshack Musundi20-Jan-14 18:35 
SuggestionRe: Neat, but ... Pinmembergerm1311-Jul-11 20:29 
GeneralRe: Neat, but ... PinmemberMeshack Musundi11-Jul-11 22:28 
Generalnice article PinmemberCIDev5-May-11 5:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web01 | 2.8.140827.1 | Last Updated 5 May 2011
Article Copyright 2011 by Meshack Musundi
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid