Multiple Validation Rules and Validation Templates in WPF

别来无恙 提交于 2019-12-05 21:12:18

Ok, so I've figured out an approach that works and I was hoping to get feedback from anyone that might have an interest in this.

ValidationRule:

My validation rule is altered to send back an "ErrorObject" that has IsRequired & Message properties

    public class ErrorObject
    {
        public bool IsRequired { get; set; }
        public string Message { get; set; }
    }

....

return new ValidationResult(false, new ErrorObject() { IsRequired = true, Message = "Is Required" });

Template:

In the Validation Template I can now access these properties and alter the visual accordingly. (In this example I'm showing an * for required fields)

            <Border
                BorderBrush="Red"
                CornerRadius="3"
                BorderThickness="1">
                <AdornedElementPlaceholder
                    x:Name="errorAdorner" />
            </Border>
            <TextBlock
                Text="*"
                Foreground="Red"
                Visibility="{Binding ElementName=errorAdorner, Path=AdornedElement.(Validation.Errors).CurrentItem.ErrorContent.IsRequired, Converter={StaticResource BooleanToVisibilityConverter}}" />

So this is a simple example, but you can imagine that this can get very powerful. Thanks MS for letting send back an object!!!

Brian Mains

I may be wrong, not 100% sure, but I think you have to programmably apply templates if you want to display varying templates.

Is this approach similar to this? Programmatically change validation rule in WPF TextBox

HTH.

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