0

I'm trying to bind a textbox with my business logic class. In the business logic class I throw an ArgumentException when the property (name is my example) is empty or null.

This creates the problem: when loading the usercontrol, the textbox is empty with no error. When I type in the textbox, and delete this text again, I get thrown out of the application with the ArgumentException from my business class. I get why he does this, but I want to prevent this from happening by showing a messagebox with the error instead.

I have tried to instead of throwing a new exeption in my business class, showing the messagebox, but when trying this out, the messagebox is also shown when loading the usercontrol. This is not what i'm looking for.

in my view:

<TextBox DockPanel.Dock="Top" Text="{Binding ManipulatedReservation.LastName, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay, ValidatesOnExceptions=True}" Name="LastName"></TextBox>
<Button Grid.Column="2" Grid.Row="2" FontSize="20" Height="45" Command="{Binding SaveCommand}" Content="{x:Static properties:Resources.Save}"></Button>

in my business class:

        private String _lastName;


        public String LastName
        {
            get { return _lastName; }
            set
            {
                SetValue(ref _lastName, value);
                if (string.IsNullOrWhiteSpace(value))
                {
                    throw new ArgumentException("Lastname cannot be empty.");

                }
            }
        }

        public static Boolean IsValid(Reservation reservation)
        {
            Boolean isValid = true;
            try
            {
                Reservation testReservation = new Reservation() { ID = 
                reservation.ID, LastName = reservation.LastName};
            }
            catch (Exception)
            {
                isValid = false;
            }
            return isValid;
        }

code behind my button:

        SaveCommand = new RelayCommand(Save, CanSave);

        private bool CanSave()
        {
            try
            {
                return Business.Reservation.IsValid(ManipulatedReservation);
            }
            catch (Exception e)
            {
                ErrorHandler.ThrowError(0, e.Message);
                return false;
            }
        }

I expect the not to recieve an error when loading to usercontrol (as it is right now), but I also don't want to get an unhanled exception when having input in my textbox and then removing it.

Bramist
  • 43
  • 2
  • 9
  • 1
    I would advise against having your validation throw an exception. "Is invalid" is a flag, not a condition that makes normal execution impossible. You might consider initializing the backing field to null, and separating the concepts of "can save" and "is valid". Null is "valid" but it can't be saved. An empty string is invalid and can't be saved. – 15ee8f99-57ff-4f92-890c-b56153 May 17 '19 at 19:21
  • It works that way, thank you. – Bramist May 18 '19 at 16:37

0 Answers0