Search code examples
c#wpfvalidationxamladorner

How to re-position adorner when resizing the window?


I have an adorner to show error messages, and the problem is the message get clipped under the window, when the window is small. So I'm trying to re-position the adorner to button or left according to the window size, or if the user resized the window.

textbox:

<TextBox IsReadOnly="False" Grid.Column="3" Grid.Row="0" Text="{Binding TextValue}" />

style:

<ControlTemplate x:Key="errorToolTipTemplate">
    <ControlTemplate.Resources>
        <Style x:Key="textblockErrorTooltip" TargetType="TextBlock">
            <Setter Property="HorizontalAlignment" Value="Center" />
            <Setter Property="VerticalAlignment" Value="Center" />
            <Setter Property="FontWeight" Value="Bold" />
            <Setter Property="Foreground" Value="White" />
            <Setter Property="Margin" Value="10 0 10 0" />
        </Style>
    </ControlTemplate.Resources>
    <DockPanel LastChildFill="true">
        <Border Height="Auto" Margin="4,0,0,0" Background="Tomato" BorderBrush="Black" BorderThickness="1" CornerRadius="2" DockPanel.Dock="Right">
            <TextBlock Style="{StaticResource textblockErrorTooltip}" Text="{Binding ElementName=customAdorner, Path=AdornedElement.(Validation.Errors)[0].ErrorContent}" />
        </Border>
        <AdornedElementPlaceholder Name="customAdorner">
            <Border BorderBrush="Red" BorderThickness="1" />
        </AdornedElementPlaceholder>
    </DockPanel>
</ControlTemplate>

<Style TargetType="{x:Type TextBox}">
    <Setter Property="Width" Value="120" />
    <Setter Property="HorizontalAlignment" Value="Left" />
    <Setter Property="Margin" Value="0,2,4,2" />
    <Setter Property="Validation.ErrorTemplate" Value="{DynamicResource errorToolTipTemplate}" />
    <!--<Setter Property="FontSize" Value="8" />-->
    <Setter Property="Background" Value="{DynamicResource entryFieldsBrush}" />
    <Style.Triggers>
        <Trigger Property="IsReadOnly" Value="True">
            <Setter Property="Background" Value="{StaticResource windowBrush}" />
        </Trigger>
        <Trigger Property="Validation.HasError" Value="True">
            <Setter Property="ToolTip" Value="{Binding Path=(Validation.Errors)[0].ErrorContent, RelativeSource={x:Static RelativeSource.Self}}" />
        </Trigger>
    </Style.Triggers>
</Style>

enter image description here


Solution

  • Using popups was the way to go for me. this link here has a working example of a popup error message.

    The Popup control provides a way to display content in a separate window that floats over the current application window relative to a designated element or screen coordinate. This topic introduces the Popup control and provides information about its use. source