0

我有一个 wpf-mvvm 应用程序。

在下面的代码中,“PartBPremiumBuydown”是一个类的实例。它有两个属性=> 1.值。和 2. HasValidationError。

属性“值”用于绑定到文本框。如果有任何验证错误...我可以设置 HasValidationError=true 吗?

  <TextBox  ToolTip="{Binding RelativeSource={RelativeSource Self}, 
                      Path=(Validation.Errors).CurrentItem.ErrorContent}">
                        <TextBox.Text>
                            <Binding Path="PartBPremiumBuydown.Value" 
                                      ValidatesOnDataErrors="True"
                                      UpdateSourceTrigger="PropertyChanged"
                             Converter="{x:Static localns:Converters.DecimalToCurrency}">
                                <Binding.ValidationRules>
                                    <localns:CurrencyRule />
                                </Binding.ValidationRules>
                            </Binding>
                        </TextBox.Text>
                    </TextBox>
4

1 回答 1

1

你应该已经PartBPremiumBuydown实现了IDataErrorInfo接口,类似于下面的代码:

public string Error { get; private set; }
public string this[string propertyName]
{
    get
    {
        string mError = string.Empty;
        if (propertyName == "Value" 
            && !<insert your rule>)
        {
            mError = "Validation error text."
        }
        Error = mError;
        return (string.IsNullOrWhiteSpace(mError))// if   NOTHING 
            ? null                                // then return null
            : mError;                             // else return error
    }
}

现在,当您将 TextBox 绑定到 时Value,如果用户输入的文本违反了您的规则,验证错误将显示在 TextBox 上。

于 2011-02-08T18:38:44.073 回答