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 available from the control.

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!

推荐.NET配套的通用数据层ORM框架:CYQ.Data 通用数据层框架
新浪微博粉丝精灵,刷粉丝、刷评论、刷转发、企业商家微博营销必备工具"