自定义文本框的 WPF 验证 ErrorTemplate

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14634318/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 23:39:47  来源:igfitidea点击:

WPF Validation ErrorTemplate for Custom TextBox

wpfvalidationcustom-controls

提问by Duke Cyrillus

Branching off of thisquestion -

这个问题的分支-

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>

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

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

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?

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

UPDATE:

更新:

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.

我发现问题出在错误模板中的 Tag 属性上。如果我删除标签,它工作得很好。

Thanks

谢谢

回答by Duke Cyrillus

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

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

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

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.

我不明白的是为什么它在使用 AdornedElement 关键字时表现不同,但在使用 RelativeSource 绑定标签/工具提示时却能正常工作。虽然问题解决了,但我欢迎任何关于为什么会发生这种情况的想法。

Thanks

谢谢