首次加载时 WPF 验证未触发 [英] WPF Validation not firing when first load

查看:40
本文介绍了首次加载时 WPF 验证未触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Prism 应用程序中,我想使用验证.我在 ViewModel 中实现了 INotifyDataError 接口,但我发现第一次加载控件时不会触发验证解决方案.

In Prism application I want to using validation.And I have implement the INotifyDataError interface in my ViewModel ,but I found that the validate solution don't be fired when the control is loaded first time.

然后我发现了同样的问题,比如wpf Validation Binding not fire on First Load"

Then I found the same Question like 'wpf Validation Binding not fired on First Load'

我找到了解决问题的解决方案WPF在第一次加载数据上下文时不触发验证是:

I Found a solution to solve the problem WPF don't fired the validation when first load the datacontext is that:

<TextBox Grid.ColumnSpan="2" Grid.Row="1" x:Name="textBoxFolder" Margin="2,4">
    <TextBox.Text>
        <Binding Path="this.MovieFolder" UpdateSourceTrigger="PropertyChanged">
            <Binding.ValidationRules>
                <!--  Validation rule set to run when binding target is updated. -->
                <Rules:MandatoryInputRule ValidatesOnTargetUpdated="True" />
            </Binding.ValidationRules>
        </Binding>
    </TextBox.Text>
</TextBox>

如您所见,ValidatesOnTargetUpdated="True" 是关键点,此属性将使 WPF 在第一次加载数据上下文时触发验证.

as you see,ValidatesOnTargetUpdated="True" is the key point ,this property will make WPF fired the validation when the datacontext load first time.

但我认为这是一个丑陋的解决方案.我需要为每个要验证的控件添加一个 Binding.ValidationRules.

But I think that's a ugly solution. I need to add a Binding.ValidationRules for every control I want to validate.

有没有什么好的方法可以解决这个问题.

Is there a good way to solve the problem.

推荐答案

好的,我已经解决了:当元素绑定到一个简单的属性时强制验证 - ValidatesOnTargetUpdated:

OK I've solved it: You force the validation when the element got bound with a simple property - ValidatesOnTargetUpdated:

 <rules:MyValidationRule ValidatesOnTargetUpdated="True"  ValidationType="notnull"/>

这篇关于首次加载时 WPF 验证未触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆