Silverlight ValidatesOnException default message localization

烂漫一生 提交于 2019-12-21 11:54:56

问题


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


回答1:


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




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/7399822/silverlight-validatesonexception-default-message-localization

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!