在 WPF 中触发数据验证

发布于 2024-08-08 05:31:32 字数 302 浏览 2 评论 0原文

我在 WPF 中遇到验证问题。
我有一个用户控件,它有几个文本框,它们绑定到数据模型。
验证是通过 IDataErrorInfo 实现的。

我希望仅当用户按下“提交数据”按钮时才触发验证,因此我将 UpdateSourceTrigger="Explicit" 与所有这些文本框的绑定一起使用。

一切工作正常,只有当用户按下按钮时才会触发验证,我会在其中更新数据源。

但是该用户控件可以隐藏或显示,当我将可见性从显示/更改为隐藏然后返回显示时,会触发验证。

有没有办法控制该阶段的验证?

I have a problem in WPF with validation.
I have a user control which has few textboxes, which are binding to datamodel.
The validation is implemented with IDataErrorInfo.

I want the validation to be triggered only when the user press the button "Submit data", so I used UpdateSourceTrigger="Explicit" with the binding of all those text boxes.

Everything working fine, and the validation only triggered when the user push the button, where I update the datasources.

But that user control can be hidden or shown, and when I changed the visibility from display/ to hidden and then back to display, the validation is triggered.

Is there a way to control the validation on that stage?

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

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

发布评论

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

评论(1

仅一夜美梦 2024-08-15 05:31:32

这是我正在使用的代码

绑定到文本框

<TextBox
        AutomationProperties.AutomationId="StreetNameTextBoxId"
        Height="20" Margin="0,0,5,0" FontSize="12" Name="_streetNameText"
        AcceptsReturn="False" AcceptsTab="False" Focusable="True"
        Text="{Binding ElementName=_this, Path=SearchParameters.EnteredAddress, UpdateSourceTrigger=Explicit}">

正在执行验证和搜索的代码(与单击名为“搜索”的按钮相关)

 private void ExecuteSearch() { 
        _streetNameText.UpdateDataSource();
        if (ViewModel.CustomerSpecification.IsValid())
            PerformActionInBackground(delegate{PerformSearch();});
    }

This is the code that I am using

The binding to the textbox

<TextBox
        AutomationProperties.AutomationId="StreetNameTextBoxId"
        Height="20" Margin="0,0,5,0" FontSize="12" Name="_streetNameText"
        AcceptsReturn="False" AcceptsTab="False" Focusable="True"
        Text="{Binding ElementName=_this, Path=SearchParameters.EnteredAddress, UpdateSourceTrigger=Explicit}">

The code that is executing the validation and search (which is associated to click of a button called "Search")

 private void ExecuteSearch() { 
        _streetNameText.UpdateDataSource();
        if (ViewModel.CustomerSpecification.IsValid())
            PerformActionInBackground(delegate{PerformSearch();});
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文