WPF验证控制模板重叠 [英] WPF Validation Control Template overlapping

查看:72
本文介绍了WPF验证控制模板重叠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有控件模板的用户控件,用于显示验证错误,验证模板:

I've got a user control with a control template to show validation errors, validation template:

<ControlTemplate x:Key="TextBoxPropertyValidationTemplate">
        <StackPanel>
            <Border BorderBrush="Red" BorderThickness="1">
                <AdornedElementPlaceholder x:Name="MyAdorner" />
            </Border>

            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>
                <Image Grid.Column="0" MaxHeight="16" MaxWidth="16"
                       Source="{Binding Source={StaticResource ValidationIcon}, Converter={StaticResource UriConverter}}" 
                       Margin="1" RenderOptions.BitmapScalingMode="HighQuality"
                       VerticalAlignment="Center" HorizontalAlignment="Center" />
                <TextBlock VerticalAlignment="Center" HorizontalAlignment="Left"
                           Text="{Binding ElementName=MyAdorner, Path=AdornedElement.(Validation.Errors)[0].ErrorContent}"
                           TextWrapping="Wrap" Grid.Column="1" FontSize="10" Foreground="Red" />
            </Grid>

        </StackPanel>
    </ControlTemplate>

我似乎无法解决一个看起来像这样的令人讨厌的问题:

And I can't seem to get around a rather irritating problem which looks like this:

我一直在尝试在用户控件上以及模板上使用一些边距,例如 Height = Auto等,但是所有这些实际上并没有帮助。有任何想法吗?

I've been trying to play around with margins on the user control and on the template also some Height=Auto etc but all these don't really help. Any ideas anyone?

如果这有助于主要用户控件(将带有验证的控件嵌套在其中)在带有AdornerDecorator的TabItem中。

If that helps the main user control (which nests the ones with validation) is in a TabItem with a AdornerDecorator.

感谢任何帮助。

推荐答案

我之所以这样说是因为您的错误消息位于 AdornerLayer ,它不参与控件的布局。 MSDN说装饰器的呈现独立于装饰器绑定到的UIElement的呈现。这就是为什么消息只是放在一切之上的原因。

I'd say this is because your error message is on the AdornerLayer, which doesn't participate in the same layout as your control. MSDN says "rendering of an adorner is independent from rendering of the UIElement that the adorner is bound to." and that is why the message is just put on top of everything.

您可以将错误文本放入原始模板中,然后根据 Validation.HasError 将其隐藏起来,并将其包含在这样的布局过程。

You could put the error text into the original template, hide it based on Validation.HasError and include it in the layout process that way.

但是,如果发生验证错误,更改控件的布局可能不是最好的方法。您可能考虑在工具提示中提供其他信息。

But changing the layout of the control might not be the best way to go if a validation error occurs. You might consider providing additional information in a ToolTip.

这篇关于WPF验证控制模板重叠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆