使用 IDataErrorInfo 并设置 Validation.HasError 样式

发布于 2024-07-27 17:34:32 字数 710 浏览 4 评论 0原文

在 WPF 中使用 IDataErrorInfo 和 Style 我想创建表单,在验证数据时可以为最终用户提供三种不同的状态 为了使场景更清晰

1)我旁边有一个文本框我有一个图标,它为最终用户提供了什么样的输入文本框期望 - 带信息图标的初始状态

2)一旦用户输入数据,它就会验证它并决定它是否有效或不 - 大多数情况下,它会显示十字 (X) 图标,表示数据无效

3) 由于在 UpdateSourceTrigger="PropertyChanged" 上进行验证,一旦验证成功,它就会将十字图标变为绿色复选标记

[         ] i (tooltip- Any valid user name )
[Ga       ] X (tooltip- Invalid user name. Must be 5 char long)
[Gaurav   ] * (it will show only correct icon, meaning valid value)

我如何使用 IDataErrorInfo 和 Style 来实现此目的,我尝试这样做,但一旦加载我的表单,它就会使所有数据无效并第一次显示十字图标。 我想为三种状态显示不同的工具提示和不同的图标(初始信息无效数据有效数据

In WPF using IDataErrorInfo and Style I want to create form where I can provide end user three different status while validating data
To make the scenario more clear

1) I have textbox next to it I have icon which provides end user what kind of input textbox expects - Initial status with information icon

2) As soon as user enter data it validates it and decides whether it is valid or not
- most of the time it will show cross (X) icon saying invalid data

3) As it is validating on UpdateSourceTrigger="PropertyChanged" it will turn cross icon to green check mark as soon as it gets validated

i.e

[         ] i (tooltip- Any valid user name )
[Ga       ] X (tooltip- Invalid user name. Must be 5 char long)
[Gaurav   ] * (it will show only correct icon, meaning valid value)

How can I achieve this using IDataErrorInfo and Style, I tried doing that but as soon as my form gets loaded it invalidates all the data and shows cross icon at the first time. I want to show different tooltip and different icon for three states (Initial info, Invalid data, Valid data)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

戏蝶舞 2024-08-03 17:34:32

IDataErrorInfo 不包含任何表示三种状态的方法。 它只知道“错误”和“无错误”。

最好的解决方案可能是通过以下方式避免使用 IDataErrorInfo:

  1. 为每个已验证字段创建一个属性,为其相关字段提供三态状态。
  2. 将视图中的 ContentControls 绑定到这些三态属性。
  3. 为所有这些新的 ContentControl 设置单一样式。
  4. 使用样式中的触发器为错误状态和有效状态分配 ContentControl.ContentTemplate 属性。 对于初始状态,在样式本身中使用 Setter 不显示任何内容。

您可能还必须为每个字段的工具提示文本创建属性。

IDataErrorInfo does not contain any means of representing three states. It only knows "error" and "no error".

The best solution may be to circumvent the use of IDataErrorInfo via the following:

  1. Create a property for each validated field that provides the three-state status for its related field.
  2. Bind ContentControls in the view to these three-state properties.
  3. Set a single style for all of these new ContentControls.
  4. Use Triggers in the Style to assign the ContentControl.ContentTemplate property for the error state and the valid state. For the initial state, use a Setter in the Style itself to display nothing.

You may also have to create properties for each field's tooltip text as well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文