Solved

WPF DataValidation and SaveCommand

Posted on 2013-01-03
6
581 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 to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Problem to picture file 3 41
Calling stored proc in EDMX 11 19
Closing all open child forms. 4 24
Birthdays 3 21
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
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…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

746 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now