WPF 搜索表单中的多字段验证器
我有一个相当简单的视图,用作搜索表单。有两个组合框和一个文本框,以及一个“搜索”按钮。如果下拉列表中没有任何选择或文本框为空,则无法执行搜索。我在应用程序的几个地方使用了 IDataErrorInfo,但它似乎不适合这里(我没有“SearchPageModel”,并且我不确定如何在视图模型上实现它),并且由于完全缺乏验证器控件,我不知道如何解决这个问题。我只想显示一条有关填写所有信息的消息(如果用户尝试在之前没有这样做的情况下进行搜索)。最简单的方法是什么?
更新: 按照第一个答案的链接中的建议,我创建了一个验证规则并将我的文本框修改为如下所示:
<TextBox Grid.Column="1" Grid.Row="3" Name="tbxPartNumber" Margin="6">
<TextBox.Text>
<Binding Path="SelectedPartNumber" UpdateSourceTrigger="PropertyChanged">
<Binding.ValidationRules>
<local:RequiredTextValidation/>
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
但是,这是一个新问题:当我转到屏幕并在文本框中输入某些内容,然后将其删除时,正如预期的那样,该框以红色突出显示。 Validation.GetHasErrors(tbxPartNumber) 返回 true。如果我转到屏幕并且根本不与文本框交互,则 Validation.GetHasErrors(tbxPartNumber) 返回 false。看来它只在我修改文本时才起作用...它不会验证用户是否只是出现并单击搜索而不输入任何内容。有办法解决这个问题吗?
I've got a fairly simple view which functions as a search form. There are two comboboxes and a textbox, with a "search" button. The search cannot be performed if there is no selection in either dropdown or if the textbox is empty. I've used IDataErrorInfo in a few places in my application but it just doesn't seem to fit here (I don't have a 'SearchPageModel', and I'm not sure how I would implement it on the viewmodel), and with a complete lack of validator controls, I'm not sure how to go about this. I just want to show a message about filling in all the information if a user tries to search without previously doing so. What's the simplest way?
UPDATE:
Following the advice in the link from the first answer, I created a validation rule and modified my textbox to look like this:
<TextBox Grid.Column="1" Grid.Row="3" Name="tbxPartNumber" Margin="6">
<TextBox.Text>
<Binding Path="SelectedPartNumber" UpdateSourceTrigger="PropertyChanged">
<Binding.ValidationRules>
<local:RequiredTextValidation/>
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
However, here is a new issue: when I go to the screen and enter something into the textbox, then delete it, the box highlights in red, as expected. Validation.GetHasErrors(tbxPartNumber) returns true. If I go to the screen and do not interract with the textbox at all, Validation.GetHasErrors(tbxPartNumber) returns false. It appears that it only works when I modify the text... it won't validate if the user just shows up and clicks search without typing anything. Is there a way around this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这篇MSDN 上的文章提供了如何验证的一个很好的示例类似的事情,请检查相应的子部分(“验证用户提供的数据”)。关键是使用
ValidationRules
并检查对话框的整个逻辑树是否有错误。编辑:设置 ValidationRule 上的
ValidatesOnTargetUpdated
="True"
应该可以解决这个问题。事实上,MSDN 文档中该属性的示例正是这种情况。此外这个答案可能在这种情况下感兴趣。编辑2:这是我拥有的导致验证失败的完整代码:
This article on MSDN gives a good example of how to validate things like that, check the respective sub-section ("Validating User-Provided Data"). The key is using
ValidationRules
and checking the whole logical tree of the dialog for errors.Edit: Setting
ValidatesOnTargetUpdated
="True"
on the ValidationRule should do the trick here. In fact the example in the MSDN documentation of that property is exactly this scenario. Further this answer might be of interest in that context.Edit2: Here's the full code i have which causes the validation to fail: