[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

WPF DataValidation and SaveCommand

Posted on 2013-01-03
6
Medium Priority
?
598 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
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…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

650 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