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 7785449
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T20:15:28+00:00 2026-06-01T20:15:28+00:00

In WPF , I want to make a TextBox that validates user input to

  • 0

In WPF, I want to make a TextBox that validates user input to only allow input that parses to a double. When the box loses focus (and the Binding updates), if the user had entered invalid input, I want the box to switch back to what it had before.

This seems like it should be simple, but I can’t get it to work. No matter what I’ve tried, the TextBox continues to display the invalid string the user entered even though the property is properly validating the input and not saving unless valid. I feel like notifying property changed on a failed parse should cause the TextBox to reset to the old value, but it doesn’t.

My view model has a property:

private double _doubleDisplayValue;
public string DoubleDisplayValue
{
    get { return _doubleDisplayValue.ToString(); }
    set
    {
        double result;
        bool success = double.TryParse(value, out result);
        if(success)
        {
            if(_doubleDisplayValue != result)
            {
                _doubleDisplayValue = result;
                NotifyPropertyChanged("DoubleDisplayValue");
            }
        }
        else
        {
            // I feel like notifying property changed here should make the TextBox
            // update back to the old value (still in the backing variable), but
            // it just keeps whatever invalid string the user entered.
            NotifyPropertyChanged("DoubleDisplayValue");
        }
    }   
}

And I set up my TextBox (I’m working in code behind):

// . . .
TextBox textBox = new TextBox();
Binding b = new Binding("DoubleDisplayValue");
b.Mode = BindingMode.TwoWay;
// assume the DataContext is properly set so the Binding has the right source
textBox.SetBinding(TextBox.TextProperty, b);
// . . .

I’ve also tried modifying the property to this, but it still doesn’t work:

private double _doubleDisplayValue;
public string DoubleDisplayValue
{
    get { return _doubleDisplayValue.ToString(); }
    set
    {
        double result;
        bool success = double.TryParse(value, out result);
        if(success)
        {
            // got rid of the if
            _doubleDisplayValue = result;
            NotifyPropertyChanged("DoubleDisplayValue");                
        }
        else
        {
            // Figured maybe I need to retrigger the setter
            DoubleDisplayValue = _doubleDisplayValue;
        }
    }   
}

What’s the best way to accomplish my goal?

  • 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-06-01T20:15:29+00:00Added an answer on June 1, 2026 at 8:15 pm

    If you really want to reset the value displayed in the TextBox you have to do something along the following lines in DoubleDisplayValue’s setter else:

    Application.Current.Dispatcher.BeginInvoke(new Action(() =>
        {
            _doubleDisplayValue = originalValue;
            NotifyPropertyChanged("DoubleDisplayValue");
        }), DispatcherPriority.ContextIdle, null);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i want to make a textbox in my wpf application which will accept only
I want to make a WPF Window that behaves like a context menu. So,
I want to make a user Control in WPf with same properties and events
If we want in our WPF application to let a user make a SQL
I've to make a textbox(WPF) for entering time with validation. I want to enter
I want to make a WPF TextBox have a DarkBlue border and thickness equal
I want to make a bigger checkbox in WPF. I've discovered that I need
I want to make the Foreground property of a WPF Textbox red as long
I want make a UI that is semi transparent in WPF VS2008, so I
I want to make a WPF control that will perform a custom behavior when

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.