How to handle exception in Value converter so that custom error message can be displayed

18,509

Solution 1

I would use a ValidationRule for that, this way the converter can be sure that the conversion works since it only is called if validation succeeds and you can make use of the attached property Validation.Errors which will contain the errors your ValidationRule creates if the input is not the way you want it.

e.g. (note the tooltip binding)

<TextBox>
    <TextBox.Style>
        <Style TargetType="{x:Type TextBox}">
            <Style.Triggers>
                <Trigger Property="Validation.HasError" Value="True">
                    <Setter Property="Background" Value="Pink"/>
                    <Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors)[0].ErrorContent}"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </TextBox.Style>
    <TextBox.Text>
        <Binding Path="Uri">
            <Binding.ValidationRules>
                <vr:UriValidationRule />
            </Binding.ValidationRules>
            <Binding.Converter>
                <vc:UriToStringConverter />
            </Binding.Converter>
        </Binding>
    </TextBox.Text>
</TextBox>

screenshot

Solution 2

I used validation and converter to accept null and numbers

XAML:

<TextBox x:Name="HeightTextBox" Validation.Error="Validation_Error">
    <TextBox.Text>
        <Binding Path="Height"
                 UpdateSourceTrigger="PropertyChanged" 
                 ValidatesOnDataErrors="True"
                 NotifyOnValidationError="True"
                 Converter="{StaticResource NullableValueConverter}">
            <Binding.ValidationRules>
                <v:NumericFieldValidation />
            </Binding.ValidationRules>
        </Binding>
    </TextBox.Text>
</TextBox>

Code Behind:

private void Validation_Error(object sender, ValidationErrorEventArgs e)
{
    if (e.Action == ValidationErrorEventAction.Added)
        _noOfErrorsOnScreen++;
    else
        _noOfErrorsOnScreen--;
}

private void Confirm_CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
    e.CanExecute = _noOfErrorsOnScreen == 0;
    e.Handled = true;
}

ValidationRule :

public class NumericFieldValidation : ValidationRule
{
    private const string InvalidInput = "Please enter valid number!";

    // Implementing the abstract method in the Validation Rule class
    public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
    {
        float val;
        if (!string.IsNullOrEmpty((string)value))
        {
            // Validates weather Non numeric values are entered as the Age
            if (!float.TryParse(value.ToString(), out val))
            {
                return new ValidationResult(false, InvalidInput);
            }
        }

        return new ValidationResult(true, null);
    }
}

Converter :

public class NullableValueConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return value;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (string.IsNullOrEmpty(value.ToString()))
            return null;
        return value;
    }
}

Solution 3

You shouldn't throw exceptions from the converter. I would implement IDataErrorInfo and implement the Error and String on that. Please check https://web.archive.org/web/20110528131712/http://www.codegod.biz/WebAppCodeGod/WPF-IDataErrorInfo-and-Databinding-AID416.aspx. HTH daniell

Share:
18,509

Related videos on Youtube

sturdytree
Author by

sturdytree

Updated on March 11, 2022

Comments

  • sturdytree
    sturdytree over 2 years

    I have a textbox that is bound to a class with a property of type Timespan, and have written a value converter to convert a string into TimeSpan.

    If a non number is entered into the textbox, I would like a custom error message to be displayed (rather than the default 'input string is in the wrong format').

    The converter code is:

        public object ConvertBack(
            object value,
            Type targetType,
            object parameter,
            CultureInfo culture)
        {
            try
            {
                int minutes = System.Convert.ToInt32(value);
                return new TimeSpan(0, minutes, 0);
            }
            catch
            {
                throw new FormatException("Please enter a number");
            }
        }
    

    I have set 'ValidatesOnExceptions=True' in the XAML binding.

    However, I have come across the following MSDN article, which explains why the above will not work:

    "The data binding engine does not catch exceptions that are thrown by a user-supplied converter. Any exception that is thrown by the Convert method, or any uncaught exceptions that are thrown by methods that the Convert method calls, are treated as run-time errors"

    I have read that 'ValidatesOnExceptions does catch exceptions in TypeConverters, so my specific questions are:

    • When would you use a TypeConverter over a ValueConverter
    • Assuming a TypeConverter isn't the answer to the issue above, how can I display my custom error message in the UI
    • slugster
      slugster about 13 years
      Errors in WPF data binding are supposed to be transparent, so that a small error doesn't kill the whole app or UI. You could log the exception, but trying to do anything else defeats the design of the databinding.
  • sturdytree
    sturdytree about 13 years
    Thanks HB, I'll give that a try
  • sturdytree
    sturdytree about 13 years
    Thanks Daniell, but I want to report exception errors rather than errors in the bound object. I believe IDataErrorInfo only allows the latter
  • Admin
    Admin almost 13 years
    Unfortunately, this only works one way. ValidationRules are only run on the trip back, from the UI to the data source. You cannot use this to intercept or handle exceptions thrown by converters in the Convert method.
  • H.B.
    H.B. almost 13 years
    @Will: I think converters should not throw any exceptions if you have a custom validation rule; when the data comes from the model it should be valid anyway and if the user enters invalid data the converter should not be called since the ValidationRule does not let it through.
  • Admin
    Admin almost 13 years
    @H.B.: But sometimes when the data comes from the model it isn't valid. Without the ability to handle exceptions in the converter, you have to do UI-related validation in the ViewModel or in your Models. For example, the View wishes to display a File as an XML file, and so must read text from the File and convert it to an XmlDocument. But what happens when a File isn't a valid XML file? Should the responsibility for checking this state for the View be pushed to the ViewModel? Breaks MVVM.
  • H.B.
    H.B. almost 13 years
    @Will: Doesn't seem like that much of a problem, you can still add an ExceptionValidationRule, no?
  • Admin
    Admin almost 13 years
    @H.B.: Doesn't work for exceptions thrown in Converters. And now we're back to my original comment....
  • Tim
    Tim over 8 years
    Thanks for this complete solution! For anyone that is having trouble getting the Validation_Error to run, make sure you include the NotifyOnValidationErrors in the XAML. Since I already had IDataErrorInfo style error validation (and thus ValidateOnDataErrors), I skimmed over the XAML and missed this detail.
  • Allan Ruin
    Allan Ruin almost 6 years
    @Tim buddy, it's NotifyOnValidationError , NOT NotifyOnValidationErrors