2011-02-08 7 views
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> 

答えて

1

あなたは以下のコードに似IDataErrorInfoインターフェース、実装PartBPremiumBuydownている必要があります、今、ユーザーが自分のルールを破ったテキストに入った場合は、ValueにあなたのTextBoxをバインドするとき

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に表示されます。

+0

[WPFを使用した複雑なビジネスデータ規則の適用](http://msdn.microsoft.com/en-us/magazine/ff714593.aspx)を参照してください。 –

関連する問題