WPF Separate Error and Warning Styles

筅森魡賤 提交于 2020-01-13 09:47:10

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/2296821/wpf-separate-error-and-warning-styles

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