Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF Separate Error and Warning Styles

How can we show user Warnings in a similar way to Errors. The only difference is that the control e.g. TextBox needs to have a different error template and similar to Validation.HasError there needs to be a Validation.HasWarning.

In other words not all validation issues are "errors" (in our application at least). We want to visually indicate whether something is a warning or error.

like image 771
Tahir Naushad Avatar asked Feb 19 '10 14:02

Tahir Naushad


1 Answers

I get the need for that middle ground sometimes. Like Commands that need 3 values from CanExecute rather than true or false.

As for warnings that operate like validation, I don't know all the pieces one would need to put together, but I think I know how one would start.

You would need to rely on attached properties and attached behaviors (attached properties that subscribe to events on the object and perform operations related to those events when they fire). You might have one that governs a collection of ValidationRule objects to use to determine whether a warning is issued or not, much like the Validation properties do. You might have one called HasWarning that gets set or unset by the validation that can be referred to in style/template triggers.

You could make the warning display part of each control's template, or you could again mimic Validation and have a WarningTemplate attached property that is used to place the warning information in an AdornerLayer.


Since custom ValidationRule objects return a ValidationResult object in which the ErrorContent is simply an object, and this object is also exposed in the ValidationError objects as ErrorContent, you may also be able to use the regular validation after all. You could possibly use a class as your ErrorContent object that has an ErrorType property of Warning or Error and bind to that in your ErrorTemplate.

I'm not sure whether having ValidationErrors present would prevent certain operations (such as button presses) you would wish to allow, but some sort of proxy on the ViewModel could be created that judges the ErrorType.

like image 53
Joel B Fant Avatar answered Nov 11 '22 23:11

Joel B Fant