WPF UpdateSourceTrigger [英] WPF UpdateSourceTrigger

查看:176
本文介绍了WPF UpdateSourceTrigger的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个TextBox绑定到一个属性,我已经将 UpdateSourceTrigger 属性设置为 Explicit 我可以控制何时验证。但是,一旦UI加载控件验证。我从来没有从后面的代码调用绑定的UpdateSource方法。我不知道为什么会发生这种情况,但我不知道如何解决它。

I have a TextBox which is bound to a property and I've set its UpdateSourceTrigger Property as Explicit so that I can control when it validates. However, as soon as the UI loads the control validates. I never call the UpdateSource method for the binding from the code behind. I'm not sure why this is happening but I do not know how to fix it.

有没有一个原因为什么 UpdateSourceTrigger =显式 不工作?

Is there a reason why UpdateSourceTrigger="Explicit" is not working?

请帮忙!
谢谢!

Please help! Thanks!

编辑:
最初TextBox为空,当被验证时,会导致验证错误。我想避免这个。我想仅在文本框失去焦点时才明确验证,或者我单击验证按钮。现在,如果我正确理解这一点,我需要做的就是调用TextBox_LostFocus处理程序中的BindingExpression的UpdateSource方法以及该按钮的Click事件处理程序。但是,我不知道如何避免初始验证,或者避免验证,直到我打到该TextBox。我使用了一个解决方法(我不喜欢),但是我更愿意找到一个更好的方法。

Initially the TextBox is empty which, when validated, causes a validation error. I want to avoid this. I want to validate explicitly only when the textbox loses focus or I click a validate button. Now, if I understand this correctly, all I need to do is call the UpdateSource method for the BindingExpression in the TextBox_LostFocus handler and the Click event handler for the button. However, I can't figure out how to avoid the initial validation, or avoid validation until I hit that TextBox. I've used a workaround(which I dislike) but I would prefer to find a better way of doing this.

我正在使用MVVM,是的。谢谢你的解释!这是非常有用的。

I am using MVVM, yes. Thank you for the explanation! It's very helpful.

进一步编辑:
是的,我使用IDataErrorInfo

Further Yes, I'm using IDataErrorInfo

推荐答案

这是正常的行为。

在DependencyProperty(即本例中为Text)中指定的绑定表达式将在元素为初始化/加载(并从源更新目标),即TextBox的Text属性设置为Source中具有指定路径的属性中的值(nb忽略当前未指定Path的情况) ))。

The binding expression specified in your DependencyProperty (i.e. Text in this case) will do an initial bind when the element is initialized/loaded (and Update the Target from the Source...i.e. the Text property of your TextBox is set to the value in the property in your Source with the specified Path (n.b. ignore the case where a Path isn't specified for now)).

UpdateSourceTrigger =显式只是停止对Target中值的自动更新(即您的TextBox的Text属性)被发送到您的Source(例如ViewModel / model)....(不要将此选项与控制目标更新相混淆)。

UpdateSourceTrigger="Explicit" just stops the automatic update of the value in the Target (i.e. the Text property of your TextBox) from being sent to your Source (e.g. a ViewModel/model) .... (don't confuse this option with controlling the Update of the Target).

在初始绑定期间,我可以想象绑定引擎看到source有一个IDataErrorInfo,因此它会检查项目是有效的...如果没有...您会收到围绕您的TextBox(由ErrorTemplate定义)....的标准错误,以便与模型中的数据的状态匹配....这就是所有

During the initial bind I'd imagine that the binding engine sees that the "source" has an IDataErrorInfo and so it checks if the "item" is valid...and if not...you get the standard error adorner surrounding your TextBox (defined by an ErrorTemplate) ....so that it matches the state of the data in your model....that's all logical.

要停止在您的视图初始加载后过大的错误指示器,您可以尝试此解决方法。

To stop the error indicators being shown too aggressively after the initial loading of your view...you could try this workaround.

您可以更改IDataErrorInfo,以便您可以控制何时执行验证检查逻辑。

You could change your IDataErrorInfo so that you are in control of when it will perform the validation checking logic.

有一个标记被设置为视图被加载...这允许IDataErrorInfo验证代码运行,而不是以前。

Have a flag that gets set after the view has been loaded...which allows the IDataErrorInfo validation code to be run...and not before.

从这一点加载后,对UpdateSource的调用将激活将会捕获无效数据的验证器(因为您的加载标志将为真,并且您的验证逻辑将会成功)。

Once loaded from that point on, calls to UpdateSource will fire the Validator which will catch invalid data (because your Loaded flag will be true, and your validation logic will do it's magic).

您可以使用以下内容: / p>

You could use something like this:

public class CustomerViewModel : IDataErrorInfo
{
    public bool DoValidation { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }

    public string Error
    {
        get
        {
            if (DoValidation)
            {
                if (error on any properties)
                     return "error on these .....";
            }
            return null; // no errors
        }
    }

    public string this[string columnName]
    {
        get
        {
            if (!DoValidation)
            {
                return null; 
            }

            string result = null;
            if (columnName == "FirstName")
            {
                if (string.IsNullOrEmpty(FirstName))
                    result = "Please enter a First Name";
            }
            if (columnName == "LastName")
            {
                if (string.IsNullOrEmpty(LastName))
                    result = "Please enter a Last Name";
            }

            return result;
        }
    }
}

将DoValidation属性设置为Loaded事件发生后的模型。

Set the DoValidation property on the model after the Loaded event has occurred.

这篇关于WPF UpdateSourceTrigger的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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