• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 600
  • Last Modified:

WPF DataValidation and SaveCommand

I have a DelegateCommand (SaveCommand) that has datavalidation performed in a CanSave method.

I have an integer Age property on a ViewModel.

DataValidation as follows:

      private void ValidateAge(int ageValue, bool throwException)
        {
           
            if (ageValue <= 20)
            {
                this.AddError("InvalidAge");
                if (throwException)
                {
                    throw new Exception("This is the exception thrown from ValidateAge method");
                }
            }
            else
            {
                this.RemoveError("InvalidAge");
            }
        }

The validation is called every time the Age property is updated, and the Save button is appropriately enabled/disabled ..... except in one situation:

If I delete all text from the TextBox used to input the age value, and then move the focus to another control, a red border surrounds the Age TextBox and the validation method is not called: this leaves the Save button in an enabled state, and when pressed calls the SaveMethod.

What am I doing wrong. This does not happen with string properties (the Save button is appropriately disabled if all text is removed from say the FirstName TextBox)
0
quentinA
Asked:
quentinA
  • 2
1 Solution
 
GangnamStyleCommented:
I suppose you are using WPF databinding and your binding source value is int (that's the Age property). The fact is when the textbox is empty, WPF will treat the value as DepedencyProperty.Undefined (or null, whatever). Before your validation logic is called, WPF needs to convert the textbox value to integer and exception occurs. You won't be able to see this exception because WPF has absorbed it, but your validation logic won't be triggered because there is no valid integer value to validate.

I am not sure if a custom ValueConverter will do the work but you may give a try.
0
 
Amandeep Singh BhullarCommented:
try putting the check for
string.IsNullOrWhiteSpace(this.agevalue)


or you can even put up code on button as

XAML
<Button Command="{Binding AuthenticateCommand}" Content="OK" Height="23" IsDefault="True" Margin="5" Width="75" />

MODAL CODE
Add and initializer Delegate command
public DelegateCommand AuthenticateCommand
{
      get;
      private set;
}

this.AuthenticateCommand = new DelegateCommand(this.OnAuthenticate, this.CanAuthenticate);

Methods
private void OnAuthenticate()
{
      //Code as per ur requirement
}

private bool CanAuthenticate()
{
 // return bollean as per your requirements
}


Hope this will help you :)
0
 
quentinAAuthor Commented:
I turned out to be related to the DelegateCommand in Prism, not acting like a RelayCommand (Josh Smith), and that either you had to add the CommandManager.RequerySuggested event to the prism delegatecommnad(ie modify the prism dll) or to RaiseCanExecuteChanged on the prism delegate command when every a propery on the viewmodel changes.
0
 
quentinAAuthor Commented:
I couldn't get the other comments to work in this scenario
0

Featured Post

Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now