开发者

WPF Validation: How to keep custom ErrorTemplate border correct size

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 Te开发者_如何学编程xtBox 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?


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜