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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T13:26:24+00:00 2026-05-28T13:26:24+00:00

Binding is so powerful in WPF. Supposed that we have a Number property (nullable

  • 0

Binding is so powerful in WPF. Supposed that we have a Number property (nullable int) and is bound to a textbox.

I realized when it throws an error, the property has the last correct value.

I mean these are the processes:

TEXTBOX: ""     PROPERTY: null
TEXTBOX: "2"    PROPERTY: 2
TEXTBOX: "2b"   PROPERTY: 2   <-- here is the problem, should be null instead 2(by the error)

Is there a way which the binding set a null value when it produce an error?

Some persons told me I need to implement IDataErrorInfo, but I guess that interface is to validate business rules. So I wouldn’t prefer user it.

UPDATE:

    <TextBox Text="{Binding Number, UpdateSourceTrigger=PropertyChanged,
        ValidatesOnExceptions=True, ValidatesOnDataErrors=True,
        NotifyOnValidationError=True, TargetNullValue={x:Static sys:String.Empty}}"
  • 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-28T13:26:25+00:00Added an answer on May 28, 2026 at 1:26 pm

    You are using UpdateSourceTrigger=PropertyChanged, which means that anytime the user hits a key, it is storing the data in your data context

    For example, user types 2, then your property is equal to "2". User types b and it will attempt to replace "2" with "2b", which fails, so the original property of "2" remains.

    Remove the UpdateSourceTrigger and it will revert to the default of LostFocus, which means it will only update the property when the TextBox loses focus.

    You could set the property to null when an error is produced, but I would not recommend doing that because then if a user accidently hits the wrong key, the TextBox would get cleared.

    As a side note, use IDataErrorInfo for all validation, not just business rule validation. WPF is built to work with it. My Models use it to verify their data is the correct length, type, etc, and my ViewModels use it to verify that business rules are being followed

    Edit

    The alternative suggestion I would have would be to bind to a string value, not a number field. This way when the value changes, you can try and cast it to your Int and return an error if it can’t be cast.

    public class SomeObject : IDataErrorInfo
    {
        public string SomeString { get; set; }
        public Int32? SomeNumber { get; set; }
    
        #region IDataErrorInfo Members
    
        public string Error
        {
            get { throw new NotImplementedException(); }
        }
    
        public string this[string columnName]
        {
            get
            {
                if (columnName == "SomeString")
                {
                    int i;
                    if (int.TryParse(SomeString, i))
                    {
                        SomeNumber = i;
                    }
                    else
                    {
                        SomeNumber = null;
                        return "Value is not a valid number";
                    }
                }
                return null;
            }
        }
    
        #endregion
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know that Binding in WPF is a really powerful feature, but I don't
WCF binding chooser algorithm I have watched a speak at a TechEd conference that
I am binding an int to a textbox. Is there a way to change
Current Situation: Direct Binding I have an NSTableView which displays the data that comes
The Binding syntax, {Binding /} , works in WPF but does not work at
I have to access the value that underlies the active cell of a DataGrid
Binding a regexp to a parameter in perl, I'm getting the following error: syntax
This binding <tk:DataGridTextColumn Binding={Binding Path=Id, Mode=OneWay} Header=Sale No. Width=1* /> Gives this error A
The Spring MVC binding mechanism is powerful, but I'm now confronted with a trivial
When binding a POJO with xmlbean, if a tag, normally with an int, is

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.