Show user error pattern hint

I created my own error template to show validation errors. Here is my XAML:

<Style TargetType="Control" x:Key="myErrorTemplate">
    <Setter Property="Validation.ErrorTemplate">
        <Setter.Value>
            <ControlTemplate>
                <DockPanel LastChildFill="True">
                    <TextBlock DockPanel.Dock="Right"
                               Foreground="Red"
                               FontSize="26"
                               FontWeight="Bold"
                               Text=" !"
                               Margin="0,-8,0,0" />
                    <Border>
                        <AdornedElementPlaceholder Name="myControl" />
                    </Border>
                </DockPanel>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Style.Triggers>
        <Trigger Property="Validation.HasError" Value="true">
            <Setter Property="ToolTip"
                    Value="{Binding RelativeSource={x:Static RelativeSource.Self},
            Path=(Validation.Errors)[0].ErrorContent}" />
        </Trigger>
    </Style.Triggers>
</Style>

This will result in an exclamation mark next to the TextBox when a validation error occurs. This template will currently display an error hint when the mouse hovers over a TextBox. I want to show a tooltip when I hover over an exclamation mark (text block). How to achieve this?

+4
source share
1 answer

Try the following:

       <TextBlock DockPanel.Dock="Right"
                           Foreground="Red"
                           FontSize="26"
                           FontWeight="Bold"
                           Text=" !"
                           Margin="0,-8,0,0" >
            <TextBlock.Style>
                  <Style TargetType="TextBlock">
                      <Style.Triggers>
                         <DataTrigger Binding="{Binding Path=(Validation.HasError), RelativeSource={RelativeSource TemplatedParent}}" Value="True">
                           <Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource TemplatedParent},
        Path=(Validation.Errors)[0].ErrorContent}" />
                       </DataTrigger>
                      </Style.Triggers> 
                  </Style>
            </TextBlock.Style>
       </TextBlock>
+1
source

Source: https://habr.com/ru/post/1545627/


All Articles