Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1096919
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T00:21:08+00:00 2026-05-17T00:21:08+00:00

I’m running into an issue with a formatting converter and data validation. I have

  • 0

I’m running into an issue with a formatting converter and data validation. I have the following textbox XAML declaration

<TextBox FontFamily="Segoe" FontSize="16" FontWeight="Medium"
    TabIndex="{Binding TabBinding}"  Foreground="Black"
    Opacity="0.9" IsTabStop="True" Uid="{Binding PriceID}"
    Text="{Binding NewPrice,Converter={StaticResource FormattingConverter},
    ConverterParameter=' \{0:C\}', Mode=TwoWay, UpdateSourceTrigger=LostFocus}"
    Background="#FFE6DED3" BorderBrush="#FFE6DED3"
    DataContext="{Binding StringFormat=\{0:c\}, NotifyOnValidationError=True}"
    Padding="0" KeyDown="TextBox_KeyDown" AcceptsReturn="False">
</TextBox>

The issue I’m up against is a data validation issue. When a user enters an invalid price (Ex: a value of “abc” or “0.0.4”), the textbox attempts to perform the conversion in the “FormattingConverter” method. (ConvertBack method pasted below) This causes an exception and the program errors out. Is there a way to delay the FormattingConverter call or bypass it if the data in the textbox is not valid?

public object ConvertBack(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture) 
{
    var objTypeConverter = System.ComponentModel.TypeDescriptor.GetConverter(targetType);
    object objReturnValue = null;

    if (objTypeConverter.CanConvertFrom(value.GetType())) {
        objReturnValue = objTypeConverter.ConvertFrom(value.ToString().Replace("$", ""));
    }

    return objReturnValue;
}

Thank you,

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T00:21:08+00:00Added an answer on May 17, 2026 at 12:21 am

    A converter’s ConvertBack always runs to convert the data to a value suitable for the target object. It is the responsibility of the converter to handle exceptions (and in the case of exception, return the original value so the binding framework will also realize it’s an invalid value).

    public object ConvertBack(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)  
    { 
        var objTypeConverter = System.ComponentModel.TypeDescriptor.GetConverter(targetType); 
        // Default return value is the original value - if conversion fails, return
        // this value so the binding framework will see an invalid value (and not 
        // just null).
        object objReturnValue = value; 
    
        if (objTypeConverter.CanConvertFrom(value.GetType())) { 
            try {
                objReturnValue = objTypeConverter.ConvertFrom(value.ToString().Replace("$", "")); 
            }
            catch( FormatException ) { }
            // Catch all of your possible exceptions and ignore them by returning the original value
        } 
    
        return objReturnValue; 
    } 
    

    You can also return DependencyProperty.UnsetValue, however in practice I prefer to see the actual error message returned in validation from an invalid value than to simply return an unset value.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.