prjndhi
prjndhi

Reputation: 1965

How does IDataErrorInfo interface provide validation values

I want to fire more than one validation on the same element. I found code but I do not understand in which situation which validation fire. The xaml code is as follows:

<ComboBox 
            x:Name="CreditedParty"
            Style="{StaticResource FormControlStyle}"
            Grid.Row="3" Grid.Column="1" Grid.ColumnSpan="2"
            ItemsSource="{Binding CreditedParties}"
            SelectedItem="{Binding Amount, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}"
            DisplayMemberPath="Name"/>
        <ContentPresenter 
            Grid.Row="4" Grid.Column="1" Grid.ColumnSpan="2"
            Content="{Binding ElementName=CreditedParty, Path=(Validation.Errors).CurrentItem}"/>

the code for vtewmodel.cs file is as follows:

   public string Error
    {
        get { return (_debitNote as IDataErrorInfo).Error; }
    }

    public string this[string columnName]
    {
        get
        {
            string error = null;

            if (columnName == "Amount")
            {
                decimal amount;

                error = ValidateAmount(out amount);
                if (!string.IsNullOrEmpty(error)) return error;

                _debitNote.Amount = amount;
                error = (_debitNote as IDataErrorInfo)["Amount"];
            }
            else if (columnName == "CreditedParty")
            {
                error = ValidateCreditedParty();
                if (!string.IsNullOrEmpty(error)) return error;
                error = (_debitNote as IDataErrorInfo)["CreditedParty"];
            }
            else if (columnName == "DebitedParty")
            {
                error = ValidateDebitedParty();
                if (!string.IsNullOrEmpty(error)) return error;
                error = (_debitNote as IDataErrorInfo)["DebitedParty"];
            }
            else
            {
                error = (_debitNote as IDataErrorInfo)[columnName];
            }

            CommandManager.InvalidateRequerySuggested();

            return error;
        }
    }

    private string ValidateAmount(out decimal amount)
    {
        amount = -1;
        string error = null;

        if (string.IsNullOrEmpty(_amountString))
            error = Strings.JournalViewModel_Amount_Missing;
        else if (!decimal.TryParse(_amountString, out amount))
            error = Strings.JournalViewModel_Amount_NaN;

        return error;
    }

    private string ValidateCreditedParty()
    {
        string error = null;

        if (_debitNote.CreditedParty == _blankCreditedParty)
            error = Strings.JournalViewModel_CreditedParty_NotSpecified;

        return error;
    }

    private string ValidateDebitedParty()
    {
        string error = null;

        if (_debitNote.DebitedParty == _blankDebitedParty)
            error = Strings.JournalViewModel_DebitedParty_NotSpecified;

        return error;
    }

    #endregion

How does this code fire more than one validation? Can any one answer.

Upvotes: 0

Views: 630

Answers (1)

Stipo
Stipo

Reputation: 4606

Fire PropertyChanged event to trigger validation.

Upvotes: 1

Related Questions