如何在 WPF 中输入错误时显示 MessageBox 并使用 MVVM 恢复 TextBox 的旧值
我曾经安静地使用过 WPF 及其内置的验证控件。我正在阅读 codeproject 中的一篇优秀文章,其链接如下
http://www. codeproject.com/KB/WPF/wpfvalidation.aspx
我的一位客户提出了一项要求,但我无法在 WPF 中使用 MVVM 来实现这一要求。
问题如下:
有一个仅接受数字的文本框。 当用户输入数字以外的任何值时,我必须向他显示错误。错误应
- 突出显示 TextBox,提供工具提示
- ,在 MessageBox 中显示错误
- 将 TextBox 恢复为其先前的值
例如,当加载 TextBox
时,它的初始值可能为 10 。然后用户输入一些错误的值,例如“aa”。
现在我必须显示一个 MessageBox,显示“值错误”,然后将值恢复为 10。
有很多文章和方法可以在 WPF 中显示错误,例如使用 ErrorTemplate 等。
<Style TargetType="{x:Type TextBox}">
<Setter Property="Validation.ErrorTemplate">
<Setter.Value>
<ControlTemplate>
<DockPanel LastChildFill="True">
<TextBlock DockPanel.Dock="Right"
Foreground="Orange"
FontSize="12pt">
!!!!
</TextBlock>
<Border BorderBrush="Green" BorderThickness="1">
<AdornedElementPlaceholder />
</Border>
</DockPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="true">
<Setter Property="ToolTip"
Value="{Binding RelativeSource={RelativeSource Self},
Path=(Validation.Errors)[0].ErrorContent}">
</Setter>
</Trigger>
</Style.Triggers>
</Style>
但是我如何恢复该值并显示消息框。我知道它可以使用代码隐藏文件中的事件来显示,但我不想在代码隐藏文件中编写任何逻辑。我想使用 MVVM 实现此功能
示例插图将非常有帮助!
I have been using WPF and its built in validation controls for quiet sometime. I was going through an excellent article in codeproject whose link is given below
http://www.codeproject.com/KB/WPF/wpfvalidation.aspx
I am having a requirement from one of our clients which I have not been able to chieve using MVVM in WPF.
The problem is as follows :
There is a TextBox which accepts only numbers.
When the user enters any value other than numbers I have to display an error to him. The error should
- highlight the TextBox, provide a ToolTip
- display an error in MessageBox
- revert the TextBox to its previous value
For example when the TextBox
is loaded, it might have an initial value say 10
. Then the user enters some wrong value say "aa".
Now I have to display a MessageBox saying "Wrong value" and then revert the value back to 10.
There are lot of articles and ways to display error in WPF say by using ErrorTemplate and so on.
<Style TargetType="{x:Type TextBox}">
<Setter Property="Validation.ErrorTemplate">
<Setter.Value>
<ControlTemplate>
<DockPanel LastChildFill="True">
<TextBlock DockPanel.Dock="Right"
Foreground="Orange"
FontSize="12pt">
!!!!
</TextBlock>
<Border BorderBrush="Green" BorderThickness="1">
<AdornedElementPlaceholder />
</Border>
</DockPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="true">
<Setter Property="ToolTip"
Value="{Binding RelativeSource={RelativeSource Self},
Path=(Validation.Errors)[0].ErrorContent}">
</Setter>
</Trigger>
</Style.Triggers>
</Style>
But how do I revert the value and display a MessageBox. I know it can be displayed using an event in code-behind file but I dont want to write any logic in my code behind file. I want to achieve this functionality using MVVM
A sample illustration will be really helpful!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
第二个问题的答案:构建一个消息框服务并从您的视图模型中调用它。您可以在所有 wpf 框架中找到 mvvm messageboxservices。以 cinch 为例。
撤消重做的事情,嗯,它内置在 wpf 中,所以 strg+z 可以工作。但我还不知道如何让它与 mvvm 一起工作:)
the answer to your 2nd question: build a messageboxservice and just call it from your viewmodel. you can find mvvm messageboxservices in all wpf frameworks. look at cinch for example.
the undo redo thing, hmm its built in in wpf, so strg+z will work. but i dont know yet how to get it work with mvvm :)
嘿人们终于解决了我的问题。其基于此
StackOverFLow 解决方案
该解决方案非常简单。我在失去 TextBox 焦点时调用验证器。验证器返回给我一个验证结果。根据结果我可以采取一些行动。
我为此使用了附加行为。我编写了一个触发器来检查 Validation.HasError 是否为真。如果这是真的,那么它会为我的附加属性分配一些虚拟值。在分配时,我会将 TextBoxBase 置于我的控制之下。使用这个我只需调用 Undo() 函数即可解决我的问题。
Hey people finally I solved the problem. Its based on this
StackOverFLow Solution
The solution is very simple. I call the Validator on losing focus from TextBox. The validator return me a Validation Result.Based on the result I can take some action.
I used attached behaviour for this. I have written a trigger which checks if Validation.HasError is true or not. If it is true, then it assigns some dummy value to my attached property. While assigning I will have TextBoxBase under my control. Using this I just call the Undo() function and it solves my problem.