In my silverlight 4 MVVM application, i can switch languages during runtime :
public void SetLanguage(string language)
{
var culture = new CultureInfo(language);
Thread.CurrentThread.CurrentUICulture = culture;
Thread.CurrentThread.CurrentCulture = culture;
// ...
}
For the inputs, i just added "ValidatesOnException=true" in case of conversion problems and it does the job. But the default exception message is in the culture of my OS and not in the manually chosen one.
In this thread on exception message localization the idea is to change CurrentCulture and CurrentUICulture, which i did. So i'm kind of stuck.
What can i do ?
Thanks :)
Edit : i tried to use a custom converter with a custom exception in the convertback method in order to validate the user's input. Problem, an exception within a convertback method is NOT caught by the validatesOnException, it breaks the application.
Edit 2 : to clarify -> if i have a decimal property bound to a textbox, and i enter "blabla" in this textbox, i want to see that there is a problem, and i want the message to be in the runtime locale and not the OS locale. I can't raise an exception in my property setter because i never get there, the default converter raises its own exception before that.
I hope it's clear. If i can help you to help me, please don't hesitate :)
One possible approach is to change the type of the property to
string
, even though you're storing adecimal
value behind it. The getter would callToString
on thedecimal
value stored, and the setter would do the conversion back fromstring
todecimal
using Decimal.Parse or similar. This approach does mean you have to do the type conversion yourself, but it does at least give you a bit more control.Your setter can throw exceptions to indicate validation errors. Alternatively, you can use one of the interfaces IDataErrorInfo and INotifyDataErrorInfo to show the validation error. This page has an example of using IDataErrorInfo, and this one has an example using INotifyDataErrorInfo.
Perhaps you aren't changing the culture at the outset.
I suggest that you try the approach given in the first answer in this link:
Change culture of Silverlight application
You can use custom implementation of ValidationRule and add to the Binding.ValidationRules collection. You'll have to clear the collection before (I am not sure how to do it XAML) and add this rule (how to do it is described in one of the MSDN page).
This class has
Validate
method, where you can perform your validation and return the error message you want.