IDataErrorInfo and not null properties binding validation

565 views Asked by At

I´m using IDataError to validate my objects properties in my controls and textboxes. In my class I added some validations and then I added the binding to the textboxes controls.

This is my class:

public class Customer : IDataErrorInfo
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public long TributaryCode { get; set; }

        #region IDataErrorInfo Members

        public string Error
        {
            get { throw new NotImplementedException(); }
        }

        public string this[string columnName]
        {
            get
            {
                string result = null;
                if (columnName == "FirstName")
                {
                    if (string.IsNullOrEmpty(FirstName))
                        result = "Please enter a First Name";
                }
                if (columnName == "LastName")
                {
                    if (string.IsNullOrEmpty(LastName))
                        result = "Please enter a Last Name";
                }
                return result;
            }
        }

        #endregion
    }

The issue I´m having is that when the TributaryCode is empty, I get an exception that says it cannot convert the value ''. This is obviosly thrown because tributarycode is a long and does not support null values. Also, when the UserControl containing the textbox loads, the value appears in 0. What I want, is to start with empty, and when the textbox is empty, to not throw that exception.

Then, what do I need to do? Do I have to write all properties as nullable? These classes then translate to requests classes of a WCF service, so this is a possible solution but I wanted to ask if this is right.

Also, as I said, all these controls are in a UserControl. I have one window that contains a ScrollViewer which contains this UserControl. The validations are done in the LostFocus event of the textboxes, but for some reason, when the window loads and loads the UserControl, all my empty TextBoxes have already the red border indicating that it didn´t pass the validation. Why can this be happening? I guess it must be something to do with the UserControl item.

This is one of my textbox XAML:

<TextBox x:Name="tbFirstName" Grid.Row="0" Grid.Column="1"
     Text="{Binding UpdateSourceTrigger=LostFocus, Path=FirstName,
            ValidatesOnDataErrors=true, NotifyOnValidationError=true}" />

Thanks!

1

There are 1 answers

3
Sheridan On BEST ANSWER

What you are experiencing is all normal.

The issue I´m having is that when the TributaryCode is empty, I get an exception that says it cannot convert the value ''

If that really bothers you, you have a number of solutions; you could initialise the value with 0; you could add a PreviewKeyDown handler to only allow long values; you could use a nullable long:

public long? TributaryCode { get; set; }

when the window loads and loads the UserControl, all my empty TextBoxes have already the red border indicating that it didn´t pass the validation

Again, this is normal... what else did you expect to happen when the properties have no values? This is called predictive validation, where the user is warned about errors before they try to save. Personally, I prefer this system as the user always knows what they can and can't do.