自定义文本框的WPF验证ErrorTemplate [英] WPF Validation ErrorTemplate for Custom TextBox

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

问题描述

这个问题开始-

将验证错误模板附加到这样的自定义文本框时-

When attaching a validation error template to my custom textbox like this -

<local:CustomTextBox CustomText="{Binding ViewModelProperty}" Validation.ErrorTemplate="{StaticResource errorTemplate}"/>

<ControlTemplate x:Key="errorTemplate">
    <DockPanel>
        <Border BorderBrush="Red" BorderThickness="1">
            <AdornedElementPlaceholder x:Name="controlWithError"/>
        </Border>
        <TextBlock Foreground="Red" FontSize="20" FontFamily="Segoe UI" Margin="3,0,0,0"  MouseDown="Exclamation_MouseDown"  Tag="{Binding AdornedElement.(Validation.Errors)[0].ErrorContent, ElementName=controlWithError}">!</TextBlock>
    </DockPanel>
</ControlTemplate>

如果ViewModelProperty中存在验证错误,我的应用程序将引发异常-

If there was a validation error in the ViewModelProperty, my application was throwing an exception -

Key cannot be null.
Parameter name: key

我不确定为什么会这样。为了将新的错误模板分配给自定义控件,是否需要做些什么?

I'm not sure why this is happening. Is there something that needs to be done in order to assign a new error template to a custom control?

更新:

我发现问题出在错误模板中的Tag属性上。如果我删除标签,它就可以正常工作。

I've figured out that the issue is with the Tag property in the error template. If I remove the Tag, it works just fine.

谢谢

推荐答案

好的,我设法解决此问题的方法是删除AdornedElement关键字并按如下所示更改错误模板:

Alright so the way I managed to fix the issue was by removing the AdornedElement keyword and changing the error template as follows:

<local:CustomTextBox CustomText="{Binding ViewModelProperty}">
    <Validation.ErrorTemplate>
        <ControlTemplate>
            <DockPanel>
                <Border BorderBrush="Red" BorderThickness="1">
                    <AdornedElementPlaceholder x:Name="controlWithError"/>
                </Border>
                <TextBlock Foreground="Red" FontSize="20" FontFamily="Segoe UI" Margin="3,0,0,0"  MouseDown="Exclamation_MouseDown">!</TextBlock>
            </DockPanel>
        </ControlTemplate>
    </Validation.ErrorTemplate>
    <local:CustomTextBox.Style>
        <Style TargetType="{x:Type local:CustomTextBox}">
            <Style.Triggers>
                <Trigger Property="Validation.HasError" Value="true">
                    <Setter Property="ToolTip" Value="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=(Validation.Errors)[0].ErrorContent}"/>
                    <Setter Property="Tag" Value="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=(Validation.Errors)[0].ErrorContent}"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </local:CustomTextBox.Style>
</local:CustomTextBox>

我不明白的是为什么它在使用AdornedElement关键字时表现不同,但在绑定时能正常工作使用相对来源的标签/工具提示。解决问题的同时,我欢迎提出任何有关此原因的想法。

What I don't understand is why it behaves differently when using the AdornedElement keyword but works fine when binding the Tag/Tooltip using the RelativeSource. While the problem is solved, I would welcome any ideas as to why this happened.

谢谢

这篇关于自定义文本框的WPF验证ErrorTemplate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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