Anastasiia Melnyk
Anastasiia Melnyk

Reputation: 139

How to show all validation errors in tooltip of polygon

I want to create validation template for textbox with border and polygon. I have next error template for textbox:

<ResourceDictionary>
<ControlTemplate x:Key="ErrorTemplate" TargetType="{x:Type Control}">
<Grid>
<Border 
        Background="#11FF0000"
        BorderBrush="#FFFF0000" 
        BorderThickness="1"
        IsHitTestVisible="False"/>
<Polygon 
        Fill="#FFFF0000" 
        Points="0,0 10,0 10,10" 
        HorizontalAlignment="Right"
        ToolTip="{Binding ElementName=adorner, Path=AdornedElement.(Validation.Errors)[0].ErrorContent}"/>
<AdornedElementPlaceholder x:Name="adorner"/>
</Grid>
</ControlTemplate>
<Style TargetType="{x:Type TextBox}">
        <Setter Property="Validation.ErrorTemplate" Value="{StaticResource ErrorTemplate}"/>
 </Style>
 </ResourceDictionary>

Example: https://prnt.sc/ltigke .

How to show all validation errors in tooltip? I know how to show all errors in listbox (http://prntscr.com/ltk6yv), but don't know how to combine it with polygon

<ControlTemplate x:Key="ErrorTemplate" TargetType="{x:Type Control}">
   <StackPanel>
      <ListBox ItemsSource="{Binding ElementName=adorner, Path=AdornedElement.(Validation.Errors)}">
         <ListBox.ItemTemplate>
            <DataTemplate>
                 <TextBlock Text="{Binding Path=ErrorContent}"/>
            </DataTemplate>
         </ListBox.ItemTemplate>
       </ListBox>
       <AdornedElementPlaceholder x:Name="adorner"/>
   </StackPanel>

Upvotes: 1

Views: 183

Answers (1)

mm8
mm8

Reputation: 169400

Set the ToolTip property to an ItemsControl:

<ControlTemplate x:Key="ErrorTemplate" TargetType="{x:Type Control}">
    <Grid>
        <Border Background="#11FF0000" BorderBrush="#FFFF0000" BorderThickness="1" IsHitTestVisible="False"/>
        <Polygon 
            Fill="#FFFF0000" 
            Points="0,0 10,0 10,10" 
            HorizontalAlignment="Right">
            <Polygon.ToolTip>
                <ItemsControl ItemsSource="{Binding}">
                    <ItemsControl.ItemTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding ErrorContent}" />
                        </DataTemplate>
                    </ItemsControl.ItemTemplate>
                </ItemsControl>
            </Polygon.ToolTip>
        </Polygon>
        <AdornedElementPlaceholder x:Name="adorner"/>
    </Grid>
</ControlTemplate>

Upvotes: 2

Related Questions