6

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?

Ben
  • 54,723
  • 49
  • 178
  • 224
adondero
  • 492
  • 2
  • 10
  • 26
  • 1
    you could use `TextBox.OnLostFocus` event – sa_ddam213 Mar 18 '13 at 22:24
  • I think he is wanting to bind a property to the LostFocus, which I don't know if this can be done – DJ Burb Mar 18 '13 at 22:25
  • Yeah I was hoping for a way to have my ViewModel do some calculations on LostFocus along with keeping the property updated as they type. Going to try and see how much of a delay I get if I just compute as they type.. might look bad as other numbers change based on what they type. – adondero Mar 18 '13 at 22:41
  • You can't set two UpdateSourceTriggers, you'll have to use the event LostFocus and update the ViewModel from your code behind – Nathan Mar 18 '13 at 22:45

3 Answers3

23

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

Ben
  • 54,723
  • 49
  • 178
  • 224
Haritha
  • 1,498
  • 1
  • 13
  • 35
5

To supplement the highest voted answer, dotnet core has migrated the interactivity library. Steps to get this working:

  1. Remove reference to "Microsoft.Expression.Interactions" and "System.Windows.Interactivity"
  2. Install the "Microsoft.Xaml.Behaviors.Wpf" NuGet package.
  3. XAML files – replace the xmlns namespaces "http://schemas.microsoft.com/expression/2010/interactivity" and "http://schemas.microsoft.com/expression/2010/interactions"with "http://schemas.microsoft.com/xaml/behaviors"
  4. C# files – replace the usings in c# files "Microsoft.Xaml.Interactivity" and "Microsoft.Xaml.Interactions" with "Microsoft.Xaml.Behaviors"

via blog (Dec 2018) post here

BurnsBA
  • 4,347
  • 27
  • 39
1

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.

Sebastian Budka
  • 396
  • 1
  • 8
  • 19
adondero
  • 492
  • 2
  • 10
  • 26
  • You can probably add a command behavior to the textbox. And only attach to textboxes that require that behavior. – TYY Mar 18 '13 at 23:29
  • I added a custom binding property to my textbox that gets set when I want this behavior on (off by default). Seems to be working – adondero Mar 19 '13 at 15:48