如何在调整 window 大小时重新定位装饰器?

How to re-position adorner when resizing the window?

我有一个显示错误消息的装饰器,问题是当 window 较小时,消息被截断在 window 下。 因此,我正在尝试根据 window 大小将装饰器重新定位到按钮或左侧,或者如果用户调整了 window.

的大小

文本框:

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

风格:

<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>

使用 popups was the way to go for me. this link here 有一个弹出错误消息的工作示例。

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