2017-06-14 5 views
1

多くのテキストボックスを持つフォームがあり、それぞれに同じ検証エラーテンプレートが必要です。 今、私はすべてのテキストボックスに対してこれらの検証エラーテンプレートを書いてはいけません。だから、どこに置かなければならないので、すべてのテキストボックスが影響を受けますか? Validation.ErrorTemplateとWPF - アプリケーション内のすべてのテキストボックス用のカスタムErrorTemplate

テキストボックス:

<TextBox x:Name="textBox3" TextWrapping="Wrap" Height="23" Text="{Binding User_Id, UpdateSourceTrigger=PropertyChanged, NotifyOnValidationError=True}" VerticalAlignment="Top"> 
<Validation.ErrorTemplate> 
    <ControlTemplate> 
    <StackPanel> 
     <AdornedElementPlaceholder x:Name="textBox"/> 
     <ItemsControl ItemsSource="{Binding}"> 
     <ItemsControl.ItemTemplate> 
      <DataTemplate> 
      <TextBlock Text="{Binding ErrorContent}" Foreground="Red"/> 
      </DataTemplate> 
     </ItemsControl.ItemTemplate> 
     </ItemsControl> 
    </StackPanel> 
    </ControlTemplate> 
</Validation.ErrorTemplate> 
</TextBox> 

マイCustomControl:

public class ValidationTextBox : TextBox 
    { 
     static ValidationTextBox() 
     { 
      DefaultStyleKeyProperty.OverrideMetadata(typeof(ValidationTextBox), new FrameworkPropertyMetadata(typeof(ValidationTextBox))); 
      //Validation.SetErrorTemplate(new ValidationTextBox(),) 
     } 
     public ValidationTextBox() { } 
    } 

答えて

2

あなたがテキストボックスのコンテナの "リソース教材" タグ内のTextBoxのための新しいスタイルを定義する必要があります。このスタイルは、コンテナ内の各テキストボックスに対して実装されます。

例:

<StackPanel> 
<StackPanel.Resources> 
<Style TargetType=TextBox> 
<Setter Property="Validation.ErrorTemplate"> 
<Setter.Value> 
<ControlTemplate> 
    <StackPanel> 
     <AdornedElementPlaceholder x:Name="textBox"/> 
     <ItemsControl ItemsSource="{Binding}"> 
     <ItemsControl.ItemTemplate> 
      <DataTemplate> 
      <TextBlock Text="{Binding ErrorContent}" Foreground="Red"/> 
      </DataTemplate> 
     </ItemsControl.ItemTemplate> 
     </ItemsControl> 
    </StackPanel> 
    </ControlTemplate> 
</Setter.Value> 
</Setter> 
</Style> 
</StackPanel.Resources 
<TextBox/> 
<TextBox/> 
<TextBox/> 
</StackPanel> 
+0

私app.xaml(Application.Ressources)にそれを置きます。それはうまくいきました。 :)カスタムコントロールは必要ありません。 – lordnik22

関連する問題