7

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 :)

Community
  • 1
  • 1
Maxime ARNSTAMM
  • 5,274
  • 10
  • 53
  • 76

3 Answers3

0

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

Community
  • 1
  • 1
Mamta D
  • 6,310
  • 3
  • 27
  • 41
  • i don't want to set the language in the constructor, i want to be able to change it at runtime. This doesn't apply, this.language is private set. – Maxime ARNSTAMM Sep 15 '11 at 12:08
  • Just because it is in the constructor does not mean it does not cannot at runtime. USe a parameterized constructor and include the logic similar to the SetLanguage method you have used – Mamta D Sep 16 '11 at 03:34
0

One possible approach is to change the type of the property to string, even though you're storing a decimal value behind it. The getter would call ToString on the decimal value stored, and the setter would do the conversion back from string to decimal 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.

Luke Woodward
  • 63,336
  • 16
  • 89
  • 104
0

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.

Karel Frajták
  • 4,389
  • 1
  • 23
  • 34