Solved

WPF DataValidation and SaveCommand

Posted on 2013-01-03
6
591 Views
Last Modified: 2013-12-17
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
Comment
Question by:quentinA
  • 2
6 Comments
 

Expert Comment

by:GangnamStyle
ID: 38777362
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
 
LVL 19

Expert Comment

by:Amandeep Singh Bhullar
ID: 38810913
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
 

Accepted Solution

by:
quentinA earned 0 total points
ID: 38894039
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
 

Author Closing Comment

by:quentinA
ID: 38908705
I couldn't get the other comments to work in this scenario
0

Featured Post

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question