2012-04-06 6 views
0

時々私はバインディングが誤った入力を示すときに、コントロールの周りに赤い枠線を表示します。どうすれば修正できますか?可能であれば、私に例を挙げてください。エラーが表示されたら、どのようにスタイルを設定できますか?

ありがとうございます。

<Binding.ValidationRules> 
    <DataErrorValidationRule/> 
</Binding.ValidationRules> 

があなたの検証ルールを定義し、障害が発生した場合にあなたはarroundの赤い四角形 を与えられます。

答えて

2

(例えばドキュメントを参照してください)。

(Validation.Errors)[0] .ErrorContentにToolTipをバインドする一般的な解決策は、エラーがない場合に多くのデバッグ・スピュー(技術用語)が発生するため、この:

[ValueConversion(typeof(ReadOnlyObservableCollection<ValidationError>), typeof(string))] 
public class ValidationErrorsToStringConverter : IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, 
     CultureInfo culture) 
    { 
     var errors = value as ReadOnlyObservableCollection<ValidationError>; 

     // If there are no errors then return an empty string. 
     // This prevents debug exception messages that result from the usual Xaml of "Path=(Validation.Errors)[0].ErrorContent". 
     // Instead we use "Path=(Validation.Errors), Converter={StaticResource ValidationErrorsConverter}". 
     if (errors == null) 
     { 
      return string.Empty; 
     } 

     var errors2 = errors.Select(e => e.ErrorContent).OfType<string>().ToArray(); 

     return errors.Any() ? string.Join("\n", errors2) : string.Empty; 
    } 
    public object ConvertBack(object value, Type targetType, object parameter, 
     CultureInfo culture) 
    { 
     throw new NotImplementedException(); 
    } 
} 

、私たちは、私が唯一つ以上がViewModel's.GetErrors(「PropertyName意味」)に存在していても、私のコンバータに渡された1件のエラーメッセージが表示さ

<converters:ValidationErrorsToStringConverter x:Key="ValidationErrorsConverter"/> 

<!-- Style to be used as the base style for all text boxes --> 
<Style x:Key="TextBoxWithValidation" TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}"> 
    <Setter Property="Validation.ErrorTemplate" Value="{StaticResource TextBoxValidationTemplate}"/> 
    <Style.Triggers> 
     <Trigger Property="Validation.HasError" Value="true"> 
      <Setter Property="Background" Value="{StaticResource BackgroundValidationBrush}"/> 
      <Setter 
       Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Mode=Self}, 
       Path=(Validation.Errors), 
       Converter={StaticResource ValidationErrorsConverter}}" 
      /> 
     </Trigger> 
    </Style.Triggers> 
</Style> 
+0

使用することができます...アイデア? – gap

0

は、私はあなたがsnipletを使用することはでき

Data Validation

ような何かを探して考えます関連付けられたコントロール。

0

添付のプロパティValidaton.ErrorTemplateTextBoxスタイルに設定します。あなたはBackgroundValidationBrushはピンクと言うことになるのTextBox

<Style x:Key="TextBoxWithValidation" TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}"> 
    <Setter Property="Validation.ErrorTemplate" Value="{StaticResource TextBoxValidationTemplate}"/> 
    <Style.Triggers> 
     <Trigger Property="Validation.HasError" Value="true"> 
      <Setter Property="Background" Value="{StaticResource BackgroundValidationBrush}"/> 
      <Setter 
       Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Mode=Self}, 
       Path=(Validation.Errors)[0].ErrorContent)}" 
      /> 
     </Trigger> 
    </Style.Triggers> 
</Style> 

のために、このような何かを行うことができ

関連する問題