使用 INotifyDataErrorInfo 添加错误信息时向下移动项目

Moving items down when adding error info using INotifyDataErrorInfo

例如,我有一个组合框,如果未选择它,那么我会在其下方显示一条错误消息。 显示消息时,它显示在组合框下方的控件顶部。 有没有办法在显示错误消息时将组合框下的控件向下移动?

<UserControl.Resources>

    <Style x:Key="ErrorStackPanel" TargetType="{x:Type StackPanel}">
        <Style.Triggers>
            <Trigger Property="Validation.HasError" Value="True">
                <Setter Property="Margin" Value="0 20 0 5"/>
            </Trigger>
        </Style.Triggers>
    </Style>

    <Style x:Key="ErrorComboBox" TargetType="{x:Type ComboBox}">
        <Setter Property="MinWidth" Value="180" />
        
        <Setter Property="Validation.ErrorTemplate">
            <Setter.Value>
                <ControlTemplate>

                    <StackPanel Orientation="Vertical" SnapsToDevicePixels="True">

                        <!--Placeholder for the TextBox itself-->

                        <AdornedElementPlaceholder x:Name="textBox" />
                        <ItemsControl ItemsSource="{Binding}">
                            <ItemsControl.ItemTemplate>
                                <DataTemplate>
                                    <TextBlock Text="{Binding ErrorContent}" 
                                               Foreground="Maroon"
                                               FontWeight="Bold"
                                                Margin="5 2 0 0"
                                                />
                                </DataTemplate>
                            </ItemsControl.ItemTemplate>
                        </ItemsControl>
                    </StackPanel>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

</UserControl.Resources>

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="auto" />
        <RowDefinition Height="auto" />
    </Grid.RowDefinitions>
    <StackPanel Grid.Row="2" Margin="10 5">
        <Label Content="Switch" />
        <ComboBox Style="{StaticResource ErrorComboBox}" ItemsSource="{Binding Switches}" SelectedItem="{Binding SelectedSwitch}" 
                    DisplayMemberPath="Name"  HorizontalAlignment="Left">
        </ComboBox>
    </StackPanel>


    <StackPanel Grid.Row="3" Margin="10 10" Style="{StaticResource ErrorStackPanel}">
        <RadioButton GroupName="pres" 
                         IsChecked="{Binding TriggerOnPress}" 
                         Content="On" Margin="0 5" />
        <RadioButton GroupName="pres" 
                         IsChecked="{Binding TriggerOnPress, Converter={valueconverters:BoolInverterConverter}}" 
                         Content="Off" Margin="0 5"/>

    </StackPanel>

StackPanel 没有错误状态。将触发器应用于 ComboBox 并更改底部边距:

<Style x:Key="ErrorComboBox" TargetType="{x:Type ComboBox}">
    <Setter Property="MinWidth" Value="180" />
    
    <Setter Property="Validation.ErrorTemplate">
        <Setter.Value>
            <ControlTemplate>
                ...
            </ControlTemplate>
        </Setter.Value>
    </Setter>

    <Style.Triggers>
        <Trigger Property="Validation.HasError" Value="True">
            <Setter Property="Margin" Value="0 0 0 20"/>
        </Trigger>
    </Style.Triggers>
</Style>