0

私はwpf-mvvmアプリケーションを持っています。

以下のコードでは、「PartBPremiumBuydown」はクラスのインスタンスです。これには2つのプロパティがあります=>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 に答える