1
votes

I have a screen with a business rules that displays some fields based on a business data and does not display others. i then play with Visibility to show/hide them.

My business object implements IDataErrorInfo.

The problem i have is that some of the validation pertains only when the field is shown.

In this code extract, the first IF makes the validation only if type_account is INTERNAL

string ValidateMinimumAmount()
{

    if (this.type_account != "INTERNAL")
       return null;

    if (this.account_minimum==null)
    {
        return "You must provide a minimum amount";
    }
    return null;
}

the problem i have is that since the initial state of my BO is NOT "Internal" then EVEN if after the user selects "INTERNAL" the validation never occurs again.

How can i "force" the validation to occur AFTER the first time. Of course i have checked that if the initial state is "Internal" then it works

3
Could you post your XAML?Suresh

3 Answers

1
votes

I'd advise you to notify errors more "properly" by adding an error property.

Here is a very understandable article which helped me, when I just started working in WPF as a complete beginner, you should take a look at this :

http://www.codeproject.com/KB/WPF/wpfvalidation.aspx

1
votes

you say that your "validation" disappear when you play with hide and show? but your object with IDataErrorInfo implementet is still not valid after show again?

then it seems its just an ValidationErrorTemplate Adorner problem. i dont know how your xaml look like, but surround your container with your controls to validate with an AdornerDecorator

<AdornerDecorator>
  <Yourcontainer with your Controls inside/>
</AdornerDecorator>

if this trick dont solve your problem post your xaml binding and idataerrorinfo implementation.

0
votes

Your question is a little confusing. Hopefully this answer will help.

In your example, you want the minimum amount to be required if the account type is internal. That implies that the validation state of the MinimumAmount property is dependent on the value of AccountType. So you have to validate MinimumAmount whenever AccountType changes. Thus:

public string AccountType
{
   get { return _AccountType; }
   set
   {
      if (_AccountType != value)
      {
         _AccountType = value;
         OnPropertyChanged("AccountType");
         ValidateMinimumAmount();
      }
   }
}

If you have a lot of interdependencies between properties, sometimes it's simplest to just implement a Validate() method for the whole object and call it whenever any property changes. Don't get hung up by the idea that it's inefficient to re-validate the entire object any time a property changes. View model properties generally only change in response to user input, and unless your Validate() method takes a significant amount of time to run, the odds are very small that it'll ever be a problem.