Search code examples
c#wpfvalidationxamlerrortemplate

how to find ErrorContent in Validation.ErrorTemplate


I have a global style for all comboboxes in application. For error validation I am using IDataErrorInfo. So when there will be error I want all textboxes to have such custom view: enter image description here with different ErrorContent (mouse should be over circle to show toolTip with error info). My combobox style is:

<Style x:Key="AlStyleTextBoxArial12" TargetType="{x:Type TextBox}">
    <Setter Property="FontFamily" Value="Arial"/>
    <Setter Property="FontSize" Value="12pt"/>
    <Setter Property="Margin" Value="3,3,3,3"/>
    <Style.Triggers>
        <Trigger Property="Validation.HasError" Value="True">
            <Setter Property="Validation.ErrorTemplate">
                <Setter.Value>
                    <ControlTemplate>
                        <DockPanel LastChildFill="True">
                            <Grid DockPanel.Dock="Right" Margin="-23,0,0,0" Width="15" Height="15">
                                <Grid.ToolTip>
                                   <ToolTip Content="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=(Validation.Errors)[0].ErrorContent}"/>
                                </Grid.ToolTip>
                                <Ellipse Fill="Red" VerticalAlignment="Stretch" HorizontalAlignment="Stretch"/>
                                <TextBlock Text="!" VerticalAlignment="Center" HorizontalAlignment="Center" Foreground="White" FontSize="11pt" FontWeight="Bold" Margin="0,0,0,1"/>
                            </Grid>
                            <Border BorderBrush="Red" BorderThickness="1">
                                <AdornedElementPlaceholder/>
                            </Border>
                        </DockPanel>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Trigger>
    </Style.Triggers>
</Style>

And my TextBox is implemented this way:

 <TextBox Text="{Binding Snr, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}"
                                             Grid.Row="3" Grid.Column="2"/>

View of the TextBox, when I have error, is ok, but I cannot set toolTip. It is always empty. Any ideas why I cann't get ErrorContent?


Solution

  • I've changed my style to this :

     <Style x:Key="AlStyleTextBoxArial12" TargetType="{x:Type TextBox}">
        <Setter Property="FontFamily" Value="Arial"/>
        <Setter Property="FontSize" Value="12pt"/>
        <Setter Property="Margin" Value="3,3,3,3"/>
        <Style.Triggers>
            <Trigger Property="Validation.HasError" Value="True">
                <Setter Property="Validation.ErrorTemplate">
                    <Setter.Value>
                        <ControlTemplate>
                            <DockPanel LastChildFill="True">
                                <Grid DockPanel.Dock="Right" Margin="-23,0,0,0" Width="15" Height="15">
                                    <Grid.ToolTip>
                                        <ToolTip Content="{Binding [0].ErrorContent}"/>
                                    </Grid.ToolTip>
                                    <Ellipse Fill="Red" VerticalAlignment="Stretch" HorizontalAlignment="Stretch"/>
                                    <TextBlock Text="!" VerticalAlignment="Center" HorizontalAlignment="Center" Foreground="White" FontSize="11pt" FontWeight="Bold" Margin="0,0,0,1"/>
                                </Grid>
                                <Border BorderBrush="Red" BorderThickness="1">
                                    <AdornedElementPlaceholder x:Name="customAdorner" ToolTip="{Binding [0].ErrorContent}"/>
                                </Border>
                            </DockPanel>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Trigger>
        </Style.Triggers>
    </Style>
    

    and it is working now.