views:

58

answers:

1

I have the following custom Validation.ErrorTemplate:

            <Setter Property="Validation.ErrorTemplate">
            <Setter.Value>
                <ControlTemplate>
                    <DockPanel LastChildFill="True">
                        <Label DockPanel.Dock="Bottom" Foreground="Red" Content="{Binding ErrorContent}" Margin="0,5,0,0" Background="LightGray"/>
                        <Border BorderBrush="Red" BorderThickness="2">
                            <AdornedElementPlaceholder/>
                        </Border>
                    </DockPanel>

                </ControlTemplate>
            </Setter.Value>
        </Setter>

The problem is when the ErrorContents are wider than say a TextBox that is using this style, the red border sizes itself to the ErrorContents instead of the original control. How do I force the border to always be the size of the control, not the ErrorContents?

+1  A: 

Change the HorizontalAlignment of the Border from the default of Stretch to something like Left. This will allow it to use its desired size, which will be the size of the placeholder plus the size of the border, rather than being forced to stretch to the width of the DockPanel.

<DockPanel LastChildFill="True">
    <Label DockPanel.Dock="Bottom" Foreground="Red" Content="{Binding ErrorContent}" Margin="0,5,0,0" Background="LightGray" />
    <Border BorderBrush="Red" BorderThickness="2" HorizontalAlignment="Left">
        <AdornedElementPlaceholder/>
    </Border>
</DockPanel>
Quartermeister
Setting DockPanel.Dock="Left" also works.
AKoran