WPF 单独的错误和警告样式 [英] WPF Separate Error and Warning Styles
问题描述
我们如何以类似于错误的方式显示用户警告.唯一的区别是控制,例如TextBox 需要有不同的错误模板,并且类似于 Validation.HasError 需要有一个 Validation.HasWarning.
换句话说,并非所有验证问题都是错误"(至少在我们的应用程序中).我们希望直观地表明某事是警告还是错误.
有时我需要中间立场.就像 Command
需要来自 CanExecute
而不是 true
或 false
的 3 个值一样.
至于像验证一样操作的警告,我不知道需要将所有部分放在一起,但我想我知道如何开始.
您需要依赖附加属性和附加行为(订阅对象事件并在事件触发时执行与这些事件相关的操作的附加属性).您可能有一个管理 ValidationRule
对象的集合,用于确定是否发出警告,就像 Validation
属性所做的那样.您可能有一个名为 HasWarning
的对象,它通过可在样式/模板触发器中引用的验证来设置或取消设置.
您可以将警告显示作为每个控件模板的一部分,或者您可以再次模仿 Validation
并具有一个 WarningTemplate
附加属性,用于将警告信息放置在一个 AdornerLayer
.
因为自定义 ValidationRule
对象返回一个 ValidationResult
对象,其中 ErrorContent
只是一个对象,并且这个对象也暴露在 ValidationResult
对象中code>ValidationError 对象为 ErrorContent
,毕竟你也可以使用常规验证.您可能会使用一个类作为您的 ErrorContent
对象,该对象具有一个 ErrorType
属性为 Warning 或 Error 并绑定到您的 ErrorTemplate
中.>
我不确定存在 ValidationError
是否会阻止您希望允许的某些操作(例如按下按钮),但是可以在 ViewModel 上创建某种代理来判断错误类型
.
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.
I get the need for that middle ground sometimes. Like Command
s 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 ValidationError
s 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
.
这篇关于WPF 单独的错误和警告样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!