WPF UpdateSourceTrigger

I have a TextBox associated with a property, and I set its UpdateSourceTriggerProperty as Explicitso that I can control when it checks. However, as soon as the user interface loads control, it is checked. I never call the UpdateSource method to bind code. I'm not sure why this is happening, but I don't know how to fix it.

Is there a reason why it UpdateSourceTrigger="Explicit"doesn't work?

Please, help! Thank!

Edit: Initially, the TextBox is empty, which during validation causes a validation error. I want to avoid this. I want to explicitly check only when the text field loses focus or I press the confirmation button. Now, if I understand this correctly, all I need to do is call the UpdateSource method for BindingExpression in the TextBox_LostFocus handler and the Click event handler for the button. However, I cannot figure out how to avoid the initial validation or avoid the validation until I hit this TextBox. I used a workaround (which I don't like), but I would prefer to find a better way to do this.

I am using MVVM, yes. Thank you for the clarification! It is very useful.

Further editing: Yes, I am using IDataErrorInfo

+3
source share
1 answer

.

, DependencyProperty (.. ), , / ( ... .. Text TextBox (nb , )).

UpdateSourceTrigger="Explicit" Target (.. Text TextBox) (, ViewModel/model).... ( ).

, , "" IDataErrorInfo , ""... ... adorner TextBox ( ErrorTemplate).... , .... .

... .

IDataErrorInfo, , .

, ... IDataErrorInfo... .

UpdateSource Validator, ( Loaded true, ).

- :

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.

+7

All Articles