Search code examples
wpferrortemplate

WPF ControlTemplate Height


I have the following style for validating input in my controls:

<Style x:Key="MyErrorTemplate" TargetType="Control">
    <Style.Setters>
        <Setter Property="Validation.ErrorTemplate">
            <Setter.Value>
                <ControlTemplate x:Name="ControlErrorTemplate">
                    <StackPanel Orientation="Vertical" Height="Auto">
                        <StackPanel Orientation="Horizontal">
                            <TextBlock Foreground="Red" FontSize="20">!</TextBlock>
                            <AdornedElementPlaceholder x:Name="Holder"/>
                        </StackPanel>
                        <Label Foreground="Red" Content="{Binding ElementName=Holder, 
                            Path=AdornedElement.(Validation.Errors)[0].ErrorContent}"/>
                    </StackPanel>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style.Setters>
</Style>

If an error happens, the error message in the label appears under the control (e.g. textbox) and overlaps the control below. I made StackPanel's Height="Auto", but it didn't help. Each control is in a Grid cell, and the Grid's row Height is also Auto. Could you please tell me what I am missing? I want the error message to push what is below down. Thanks.


Solution

  • Validation.ErrorTemplate shows error feedback on an adorner layer. This means all controls in this template will not be considered when the layout system is measuring and arranging the controls on the adorned element layer.