views:

208

answers:

4

Hi,

I have a textbox bound to a property. The property continously gets updated from a timer. What I'm trying to do is to make it so that when I'm in the middle of typing something in the textbox, the textbox should stop updating itself from the property. Is there a way to do that?

Thanks!

+2  A: 

I'm not a WPF or databinding expert, so there may be a better way, but I'd say you'll have to handle the GotFocus and LostFocus events and add/remove the databinding in those event handlers.

Max Schmeling
Yes, that's right! That's a good idea.
Steve the Plant
A: 

You'll probably find this previous StackOverflow question helpful: http://stackoverflow.com/questions/671728/net-textbox-control-wait-till-user-is-done-typing. You should be able to modify that fairly easily to do what you need.

B.R.
+1  A: 

I'd do something in the order of:

  public void Timer_Tick(object sender,EventArgs eArgs)
  {
    if(!Textbox.GotFocus())
    {
          // Regular updating of textbox  
    }
  }
taserian
A: 

If you have access to the Binding object, you could set its UpdateSourceTrigger property to Explicit, which will prevent automatic updates.

EDIT

Perhaps something like this

UpdateSourceTrigger old;

protected override void OnGotFocus(RoutedEventArgs e)
{
    Binding b = BindingOperations.GetBinding(textBox1, TextBox.TextProperty);
    old = b.UpdateSourceTrigger;
    b.UpdateSourceTrigger = UpdateSourceTrigger.Explicit;
}

protected override void OnLostFocus(RoutedEventArgs e)
{
    Binding b = BindingOperations.GetBinding(textBox1, TextBox.TextProperty);
    b.UpdateSourceTrigger = old;
}

Of course, this is short form, without any null checking etc.

kek444
Hmmm. Your idea combined with Max Schmeling's suggestion might be a good lead to a solution.
Steve the Plant
Well, it doesn't sound too hard to add GotFocus and LostFocus handlers in which you would set this property on a retrieved Binding object.
kek444
Edited my answer after coming up with a bit of code.
kek444