0

我想在同一个元素上触发多个验证。我找到了代码,但我不明白在哪种情况下会触发验证。xml代码如下:

<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}"/>

vtewmodel.cs 文件的代码如下:

   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

此代码如何触发多个验证?谁能回答。

4

1 回答 1

1

触发PropertyChanged事件以触发验证。

于 2012-05-21T07:54:26.803 回答