0

Binding to double may produce following validation error

Value ... could not be converted.

When using ExceptionValidationRule errors are more talkative:

Input string was not in a correct format.

Value was either too big or too small for a ...

Perhaps there are more. Add to them those what I can throw myself in bound property setter.

Now, I want to localize those messages (preferably second version). Not a big surprise, but sources doesn't reveal anything useful (am I looking at wrong place?).

I can make my own validation rules, but perhaps there is an easier way?

My question: can I localize ExceptionValidationRule?

Below is mcve:


xaml:

<TextBox>
    <TextBox.Text>
        <Binding Path="TestDouble" UpdateSourceTrigger="PropertyChanged">
            <Binding.ValidationRules>
                <!--<local:MyValidationRule />-->
                <!--<ExceptionValidationRule />-->
            </Binding.ValidationRules>
        </Binding>
    </TextBox.Text>
    <Validation.ErrorTemplate>
        <ControlTemplate>
            <TextBlock Margin="0,20,0,0" Foreground="Red" Text="{Binding ErrorContent}" />
        </ControlTemplate>
    </Validation.ErrorTemplate>
</TextBox>

cs:

public partial class MainWindow : Window
{
    public double TestDouble { get; set; }

    public MainWindow()
    {
        InitializeComponent();
        DataContext = this;
    }
}

public class MyValidationRule : ValidationRule
{
    public override ValidationResult Validate(object value, CultureInfo cultureInfo) => ValidationResult.ValidResult; // not used

    public override ValidationResult Validate(object value, CultureInfo cultureInfo, BindingExpressionBase owner)
    {
        var bindingExpression = owner as BindingExpression;
        if (bindingExpression != null)
        {
            var type = bindingExpression.ResolvedSource.GetType().GetProperty(bindingExpression.ResolvedSourcePropertyName).PropertyType;
            if (type == typeof(double))
            {
                double result;
                if (!double.TryParse((string)value, out result))
                    return new ValidationResult(false, "The value format is not recognized"); // custom message
            }
            ... // and so on, for all types ?????
        }
        return base.Validate(value, cultureInfo, owner);
    }
}
Community
  • 1
  • 1
Sinatr
  • 20,892
  • 15
  • 90
  • 319
  • exception messages are localized out of box (http://stackoverflow.com/questions/209133/exception-messages-in-english) – ASh Mar 21 '17 at 14:21
  • @ASh, I forgot to mention what localization will be done by professional translators and its customer-oriented (they shouldn't know about `double`, etc.), think about it as customizing messages texts. My problem is how to deal with validation errors (I am unable control them when user is typing), going `MyValidationRule` seems very tedious to me... – Sinatr Mar 21 '17 at 14:25
  • Obviously you cannot change the way the built-in ExceptionValidationRule class is implemented. What's wrong with implementing your own custom validation rule? You can put any translation logic you want inside its Validate method. This would be the correct way of doing this because the setter of the source property will never get hit when you type in an invalid value. You can only set a double property to a double value and nothing else. – mm8 Mar 21 '17 at 17:00
  • @mm8, see my answer, as I though custom validation rule was really *wrong* way to customize such messages (it's doable this way, but why if `ExceptionValidationRule` is already doing it). – Sinatr Mar 22 '17 at 10:23

1 Answers1

0

To localize such error messages, which appears during updating source of binding, its enough to setup UpdateSourceExceptionFilter callback handler of binding, no custom validation rule is needed (ExceptionValidationRule is required).

xaml:

<Binding UpdateSourceExceptionFilter="ExeptionFilter" ...>

cs:

object ExeptionFilter(object bindingExpression, Exception exception)
{
    return "Localized error message";
}

Obviously it can be switch/case (in C# 7, earlier - if/else if) to provide exception-specific messages (in my question those are FormatException and OverflowException correspondingly).

Sinatr
  • 20,892
  • 15
  • 90
  • 319