Wpf 我们可以在验证时设置源对象的属性吗?

Wpf 我们可以在验证时设置源对象的属性吗?,wpf,mvvm,validationrules,Wpf,Mvvm,Validationrules,我有一个wpf mvvm应用程序 在下面的代码中,“PartBPremimBuydown”是一个类的实例。它有两个属性=>1。价值二,。HasValidationError 属性“Value”用于绑定到textbox。如果存在任何验证错误…我可以将HasValidationError设置为true吗 <TextBox ToolTip="{Binding RelativeSource={RelativeSource Self}, Path=

我有一个wpf mvvm应用程序

在下面的代码中,“PartBPremimBuydown”是一个类的实例。它有两个属性=>1。价值二,。HasValidationError

属性“Value”用于绑定到textbox。如果存在任何验证错误…我可以将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>

您应该让
partbpremimbuydown
实现
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
    }
}
公共字符串错误{get;private set;}
公共字符串此[string propertyName]
{
得到
{
string mError=string.Empty;
如果(propertyName==“值”
&& !)
{
mError=“验证错误文本。”
}
错误=错误;
return(string.IsNullOrWhiteSpace(mError))//如果没有
?null//然后返回null
:mError;//否则返回错误
}
}
现在,当您将文本框绑定到
值时,如果用户输入的文本违反了您的规则,则验证错误将显示在文本框上。

请参阅