2016-12-22 11 views
-1

カスタムバリデータとエラーテンプレートを作成しました。エラーテンプレートは以下のとおりです。カスタム検証エラーメッセージが他のコントロールと重複しています

<ControlTemplate x:Key="errorTmp"> 
     <Grid> 
      <Grid.RowDefinitions> 
       <RowDefinition Height="auto"></RowDefinition> 
       <RowDefinition Height="auto"></RowDefinition> 
      </Grid.RowDefinitions> 
      <Border Grid.Row="1" BorderBrush="Red" BorderThickness="1"> 
       <AdornedElementPlaceholder x:Name="Adorner"/> 
      </Border>     
      <TextBlock Grid.Row="0" Foreground="Red" Text="{Binding ElementName=Adorner, Path=AdornedElement.(Validation.Errors)[0].ErrorContent}" Margin="0,0,0,5"></TextBlock> 
     </Grid> 
    </ControlTemplate> 

他のコントロールでエラーメッセージが重複しています。

enter image description here

+0

私はこれがアドルナー層のためだと知っています。そして、私はツールチップにエラーメッセージを表示したくないので、エラーメッセージのためのスペースを作るためにコントロールの高さを調整する必要があります。私はウェブ上で多くを検索しましたが、解決策は見つかりませんでした。 – Rudra

答えて

1

あなたはadorner層内の要素のためのスペースを確保する必要があります。 Validation.HasErrorプロパティがtrueを返すとき、TextBoxコントロール自体のMarginプロパティを増やすことでこれを行うことができます。 adornerのレンダリングはレンダリングから独立しているため

<TextBox /> 

<TextBox Text="{Binding Username, UpdateSourceTrigger=PropertyChanged, ValidatesOnNotifyDataErrors=True}"> 
    <Validation.ErrorTemplate> 
     <ControlTemplate> 
      <Grid> 
       <Grid.RowDefinitions> 
        <RowDefinition Height="20"></RowDefinition> 
        <RowDefinition Height="auto"></RowDefinition> 
       </Grid.RowDefinitions> 
       <Border Grid.Row="1" BorderBrush="Red" BorderThickness="1"> 
        <AdornedElementPlaceholder x:Name="Adorner"/> 
       </Border> 
       <TextBlock Grid.Row="0" Foreground="Red" Text="{Binding ErrorContent}"></TextBlock> 
      </Grid> 
     </ControlTemplate> 
    </Validation.ErrorTemplate> 
    <TextBox.Style> 
     <Style TargetType="TextBox"> 
      <Style.Triggers> 
       <Trigger Property="Validation.HasError" Value="True"> 
        <!-- increase the top margin of the TextBox in order for the error content not to overlap the control above --> 
        <Setter Property="Margin" Value="0 20 0 0" /> 
       </Trigger> 
      </Style.Triggers> 
     </Style> 
    </TextBox.Style> 
</TextBox> 

:あなたのValidation.ErrorTemplateにおけるグリッドの最初の行と同じ高さにテキストボックスの上マージンを設定することもでき、この場合には

adornerがバインドされているUIElementのうち、adornerが表示されているときにTextBoxがその位置を自動的に調整する方法はありません。だからこそadorner要素のスペースを明示的に自分で確保しなければならないのです。

関連する問題