WPF Validation ErrorTemplate for Custom TextBox

放肆的年华 提交于 2019-12-04 04:43:57

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>

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.

Thanks

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!