Search code examples
wpfxamlbindingtextboxprism

Textbox Binding to both LostFocus and Property Update


Currently I bind to my TextBoxes as:

Text="{Binding DocValue,
         Mode=TwoWay,
         ValidatesOnDataErrors=True,
         UpdateSourceTrigger=PropertyChanged}"

This works great in getting every keystroke to do button status checking (which I want).

In addition, I would like to track the LostFocus event on the TextBox (through binding) and do some additional calculations that might be too intensive for each keystroke.

Anyone have thoughts on how to accomplish both?


Solution

  • I think I have found a solution... I created a composite command and use that for the additional communication.

    Command definition

    public static CompositeCommand TextBoxLostFocusCommand = new CompositeCommand();
    

    My textbox

    private void TextboxNumeric_LostFocus(object sender, RoutedEventArgs e)
    {
        if (Commands.TextBoxLostFocusCommand.RegisteredCommands.Count > 0)
        {
            Commands.TextBoxLostFocusCommand.Execute(null);
        }
    }
    

    then in my ViewModel, I create a Delegate Command and wire to it..

    It seems like it is working, wonder if there is a better way. One downfall to this is that every Textbox will fire this, not just my items that are attached to formulas I want to calculate. Might need to think of ways to improve on this.