15.02.2015 Views

C# 4 and .NET 4

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

574 ❘ ChaPTer 22 lOcAlizAtiOn<br />

ci = new CultureInfo("es-ES");<br />

Thread.CurrentThread.CurrentCulture = ci;<br />

Console.WriteLine("{0}: {1}", ci.ToString(), d.ToString("D"));<br />

The output of this example program shows ToLongDateString() with the current culture of the thread, a<br />

French version where a CultureInfo instance is passed to the ToString() method, <strong>and</strong> a Spanish version<br />

where the CurrentCulture property of the thread is changed to es-ES:<br />

Tuesday, June 02, 2009<br />

mardi 2 juin 2009<br />

en-US: Tuesday, June 02, 2009<br />

es-ES: martes, 02 de junio de 2009<br />

Cultures in action<br />

To see all cultures in action, you can use a sample Windows Presentation Foundation (WPF) application<br />

that lists all cultures <strong>and</strong> demonstrates different characteristics of culture properties. Figure 22-4 shows the<br />

user interface of the application in the Visual Studio 2010 WPF Designer.<br />

figure 22-4<br />

During initialization of the application, all available cultures are added to the tree view control that is<br />

placed on the left side of the application. This initialization happens in the method AddCulturesToTree(),<br />

which is called in the constructor of the Window class CultureDemoWindow:<br />

public CultureDemoWindow()<br />

{<br />

InitializeComponent();<br />

}<br />

AddCulturesToTree();<br />

code snippet CultureDemo/MainWindow.xaml.cs<br />

In the method AddCulturesToTree(), you get all cultures from the static method CultureInfo<br />

.GetCultures(). Passing CultureTypes.AllCultures to this method returns an unsorted array of all<br />

available cultures. The array is sorted using a Lambda expression that is passed to the Comparison delegate<br />

of the second argument of the Array.Sort() method. Next, in the foreach loop, every single culture is<br />

added to the tree view. A TreeViewItem object is created for every single culture because the WPF TreeView<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!