Solved

WPF DataValidation and SaveCommand

Posted on 2013-01-03
6
588 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
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 Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
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…

776 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