Validation against focus is lost

We are creating a new application and one of the requirements is "no save button". Therefore, every property change is saved. To reduce the number of db transactions, we would like to keep the TextBox text after losing focus.

Our problem is validation, we usually use something like this:

[Required(AllowEmptyStrings = false, ErrorMessage = "Email is required")]
[EmailAddress(ErrorMessage = "This is not valid email address")]
public string UserEmail
{
    get { return userEmail; }

    [DebuggerNonUserCode]
    set
    {
        Validator.ValidateProperty(value, new ValidationContext(this, null, null) 
                                { MemberName = "UserEmail" }
                                );

        userEmail = value;
        NotifyOfPropertyChange(() => UserEmail);
    }
}

      

This is only for

   binding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;

      

But in the new solution, we would like to introduce a setter of the UserEmail property only when the textbox loses focus. But what about validation, it should work on every key press, how to do it?

+3


source to share


1 answer


you can take a look at the System.Windows.Interactivity.WPF library , which includes an Interaction class that acts as an attached behavior. It allows you to essentially subscribe to which events you need to subscribe to. This will allow you to perform property validation using the binding, as you do now, and then make the actual DB call when the input field loses focus. For a text box, it might look something like this:

<TextBox Text="{Binding TextField, UpdateSourceTrigger=PropertyChanged}">
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="LostFocus">
            <i:EventTrigger.Actions>
                <i:InvokeCommandAction Command="{Binding SaveCommand}"/>
            </i:EventTrigger.Actions>
        </i:EventTrigger>
    </i:Interaction.Triggers>
</TextBox>

      



So, in essence, you "de-pair" can save the call from the actual "setter" of the property. Something like this is possible:

    private string textField;
    public string TextField
    {
        get { return textField; }
        set
        {
            if (value == textField) return;
            textField = value;
            OnPropertyChanged();
        }
    }

    private ICommand saveCommand;

    public ICommand SaveCommand
    {
        get { return saveCommand ?? (saveCommand = new DelegateCommand(Save)); }
    }

    private void Save()
    {
       // Do your DB transactions here. 
    }

      

0


source







All Articles