Input Localization in Silverlight without IValueConverter

 

The Problem

There are plenty of articles on the web detailing how to localize strings within Silverlight applications, however, one area of confusion seems to be how to get Silverlight to accept text input using the users current culture settings.

For example, if I type 10,1 in a locale that uses the ‘,’ symbol as the decimal symbol I might expect to get the decimal number “10.1” (UK Format), however, I will in fact receive the number 101.

Many examples demonstrate using custom IValueConverter implementations that format and parse values using the current culture to work around this issue , however, this method is tedious, error prone, and unnecessary.

Solution

By default, bindings in Silverlight that do not specify a custom ValueConverter parse input text using the culture specified by the target elements Language property. The Language property is of type XmlLanguage and is one of the few built in inherited properties supported in Silverlight. This means that setting the Language property of the RootVisual will cause all text to be parsed using the users current culture.

One thing to keep in mind is that by default Popup (and descendents such as ChildWindow) do not belong to the root visual tree and will therefore need to be configured independently (or added to the visual tree).

Below is a simple class that returns an XmlLanguage object for the current culture

   1: public class CultureSettings
   2: {
   3:   public XmlLanguage Language
   4:   {
   5:      get
   6:      {
   7:         return XmlLanguage.GetLanguage(Thread.CurrentThread.CurrentCulture.Name);
   8:      }
   9:   }
  10: }

The class is added to the Applications resources

   1: <Application.Resources>
   2:     <ResourceDictionary>
   3:         <Windows:CultureSettings x:Key="CultureSettings"/>
   4:     </ResourcesourceDictionary>
   5: </Application.Resources>
   6:  

And finally bound to the Language property of the root visual

   1: <ContentPresenter x:Name="RootVisual" Language="{Binding Language, Source={StaticResource CultureSettings}}"/>
2 Comments Filed Under [ Silverlight ]

Comments

#  Twitter Trackbacks for Input Localization in Silverlight without IValueConverter [imeta.co.uk] on Topsy.com
Gravatar
Twitter Trackbacks for

Input Localization in Silverlight without IValueConverter
[imeta.co.uk]
on Topsy.com
Left by Pingback/TrackBack on 10/2/2009 11:10 AM
# <br /> Unhandled Error<br />
Gravatar <br /> Unhandled Error<br />
Left by Pingback/TrackBack on 5/2/2010 9:53 PM

Leave Your Comment

Title*
Name*
Email (never displayed)
 (will show your gravatar)
Url
Comment*

Please add 5 and 3 and type the answer here:

Preview Your Comment.