- 追加された行はこの色です。
- 削除された行はこの色です。
&tag(WPF/Validation);
*目次 [#e2e21625]
#contents
*参考情報 [#p48e9e36]
-[[Validation in Windows Presentation Foundation - CodeProject:http://www.codeproject.com/KB/WPF/wpfvalidation.aspx]]…WPFのValidationの基本。
-[[Attributes-based Validation in a WPF MVVM Application - CodeProject:http://www.codeproject.com/KB/smart/WPF_Validation_Attribute.aspx]]
*方法 [#nba9c908]
**例外を使う [#k7147546]
**ValidationRuleを使う [#a4c4baac]
**IDataErrorInfoを使う [#add19a76]
**その他の方法 [#nefdb4b0]
***Validation Attributeを使う [#d1bf0762]
-[[Automatically validating business entities in WPF using custom binding and attributes - CodeProject:http://www.codeproject.com/KB/WPF/wpf_custom_validation.aspx]]…ValidationBindingというクラスを通してバインディングすることで自動的にValidationをかける方法。各プロパティにValidation Attributeを使う。
-[[WPF Validation with Attributes and IDataErrorInfo interface in MVVM - Unknown Recipes:http://weblogs.asp.net/marianor/archive/2009/04/17/wpf-validation-with-attributes-and-idataerrorinfo-interface-in-mvvm.aspx]]…各プロパティにValidation Attributeを記述し、IDataErrorInfoでエラーを発生させる。
*Tips [#eef392a1]
**複数のエラーをまとめる [#sc8ebec8]
-[[wpf - How can I define a IDataErrorInfo Error Property for multiple BO properties - Stack Overflow:http://stackoverflow.com/questions/2112143/how-can-i-define-a-idataerrorinfo-error-property-for-multiple-bo-properties]]
-[[WPF Validation for the whole form - Stack Overflow:http://stackoverflow.com/questions/104520/wpf-validation-for-the-whole-form]]
**ValidationErrorでエラー発生 [#qc3afada]
-[[Nick Zhebrun: WPF bug with ValidationError:http://zhebrun.blogspot.com/2008/03/wpf-bug-with-validationerror.html]]
**TabItemでエラーが消える [#z910e169]
-[[WPF Validation Errors Disappear Inside TabControl When Switching TabItems « Karl On WPF – .Net:http://karlshifflett.wordpress.com/2008/02/19/wpf-validation-errors-disappear-inside-tabcontrol-when-switching-tabitems/]]
**一括Validation [#udace03a]
-[[Detecting WPF Validation Errors - Stack Overflow:http://stackoverflow.com/questions/127477/detecting-wpf-validation-errors]]
-[[wpf - GetLocalValueEnumerator() Not Returning All Properties - Stack Overflow:http://stackoverflow.com/questions/338522/getlocalvalueenumerator-not-returning-all-properties]]
**プログラムでエラー表示する [#j6bc3171]
BindingExpressionを取得してValidation.MarkInvalidを呼び出す。
BindingExpression expression = BindingOperations.GetBindingExpression(parent, entry.Property);
System.Windows.Controls.Validation.MarkInvalid(expression, new ValidationError(rule, expression, result.ErrorContent, null));
**エラーをクリアーする [#k73d6c91]
Validation.ClearInvalid
**複数コントロールに関するエラーを表示する [#qa887b00]
ViewModelでIDataErrorInfoを実装している場合は、Errorメソッドで適当なエラー文字列を返し、それを表示するだけでよい。
''XAML''
<TextBlock Text="{Binding Error}" Grid.Row="4" Grid.ColumnSpan="2" />