Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 851
adondero Avatar asked Mar 18 '13 22:03

adondero


1 Answers

Bind a command to the TextBox LostFocus event.

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>

View Model

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            
}

you have to reference System.Windows.Interactivity for this. And you have to install a redistributable to use this library. you can download it from here

like image 106
Haritha Avatar answered Sep 30 '22 17:09

Haritha