Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF Display TextBlock with validation error message below control

Is there a way to display the error contents in a TextBlock below the control similar to how the following sets the Tooltip to contain the error text?

        <Style x:Key="textBoxInError" TargetType="Control">
        <Setter Property="Validation.ErrorTemplate">
            <Setter.Value>
                <ControlTemplate>
                    <DockPanel>
                        <TextBlock DockPanel.Dock="Left" Foreground="Red" FontWeight="Bold">*</TextBlock>
                        <TextBlock Text="WOULD LIKE TO SHOW WHAT TOOLTIP IS SHOWING" DockPanel.Dock="Bottom" Foreground="Red"/>
                        <Border BorderBrush="Red" BorderThickness="2">
                            <AdornedElementPlaceholder/>
                        </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>

In other words, I rather show the error message in a TextBlock below the control instead of a Tool Tip.

like image 694
AKoran Avatar asked Jul 29 '10 12:07

AKoran


1 Answers

The DataContext of the ErrorTemplate is already the value of Validation.Errors, so you can just do:

<TextBlock Text="{Binding [0].ErrorContent}" DockPanel.Dock="Bottom" Foreground="Red"/>

or

<TextBlock Text="{Binding ErrorContent}" DockPanel.Dock="Bottom" Foreground="Red"/>
like image 196
Quartermeister Avatar answered Oct 16 '22 19:10

Quartermeister