Associating a text field with both LostFoucus and updating properties

I am currently getting attached to my TextBoxes like:

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

This works great when every keystroke performs a button status check (what I want).

In addition, I would like to track the event LostFocuson TextBox(via the binding) and perform some additional calculations that may be too intense for each keystroke.

Anyone have thoughts on how to accomplish both?

+5
source share
2 answers

I think I found a solution ... I created a composite team and used it for additional communication.

Team definition

public static CompositeCommand TextBoxLostFocusCommand = new CompositeCommand();

My text box

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

ViewModel .

, , , . , , , , . , , .

+1

TextBox LostFocus.

XAML

xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"

<TextBox Margin="0,287,0,0">
     <i:Interaction.Triggers>
          <i:EventTrigger EventName="LostFocus">
               <i:InvokeCommandAction Command="{Binding LostFocusCommand}" />
          </i:EventTrigger>
     </i:Interaction.Triggers>
</TextBox>

private ICommand lostFocusCommand;

public ICommand LostFocusCommand
{
    get
    {
        if (lostFocusCommand== null)
        {
            lostFocusCommand= new RelayCommand(param => this.LostTextBoxFocus(), null);
        }
        return lostFocusCommand;
     }
}

private void LostTextBoxFocus()
{
    // do your implementation            
}

System.Windows.Interactiviy. .

+18

All Articles