Click here to Skip to main content
Click here to Skip to main content
Go to top

Text Box with Delayed Binding

, 6 Oct 2009
Rate this:
Please Sign up or sign in to vote.
A TextBox that waits a user defined interval of time after the user has stopped typing to update its binding source

Introduction

Today I found myself writing a Search interface, I was searching a large amount of data and wanted the user to just be able to type their search query and not have to hit a button to execute the search. At first I tried using a straight WPF textBox with UpdateSourceTrigger set to PropertyChanged, however this was executing the search functionality on each keystroke, which was too slow. What I wanted was for the search to be executed when the user finished typing.

Solution

I have derived a subclass of TextBox which has a property DelayTime. As the user enters a keystroke a timer is started, if the timer reaches the DelayTime before another keystroke (set in milliseconds) the DataBinding source is updated. In the application, I am working on allowing users to type away and when they stop typing, the search is executed. I have included a few other little features; the binding will update if the text box loses focus and also when the enter or return key is pressed.

Using the Code 

The included project shows how to use the DelayedBindingTextBox, which is basically the same as using a TextBox, except you can set a DelayTime (in milliseconds).

<local:DelayedBindingTextBox
            DelayTime="500"
            Text="{Binding Path=MyTextProperty}" />

If the Text property has its binding set to UpdateSourceTrigger=PropertyChanged, the behaviour will not be any different from a standard TextBox.

In the (very ugly - I'm not a designer) screenshot below, you can see that although the text in the textbox has changed to "original text has changed", the text below (which represents what the text box is bound to) is still "original text", as the timer has not reached 3886 ms. As soon as it does, the text below the box will be updated.

screenshot.png

Points of Interest

The override of the OnTextChanged method in the DelayedbindingTextBox is worth a mention, just to have a look at the anonymous method with an enclosed call to a delegate method. Anonymous methods are so powerful, and can be just as confusing as any code I have ever read.

History

  • 01/10/09: Initial posting
  • 05/10/09: Made changes to deal with the situation when the textbox is unloaded or the binding information changes before the update is made. As this control uses threading, there exists the possibility that the binding can become invalid before the timer expires.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Aran Mulholland
Software Developer
Australia Australia
Is this programmer guy you know...
 
He writes in C#, WPF and does some audio bits and pieces for the iPhone.
 
He is into electronic music, and loves a good drum machine in the morning and a synthesizer before bed.
 
Is using twitter - @aranmul
 
He has a site for some test projects for using Core Audio on the iPhone here
 
He is compiling a collection of books he has found useful/interesting here

Comments and Discussions

 
GeneralSweet! Pinmemberkswanwick6-Oct-09 12:22 
GeneralAlternatively... PinmemberRichard Deeming6-Oct-09 5:54 
GeneralRe: Alternatively... PinmemberVitaly BBB20-Dec-11 20:43 
GeneralLovely PinmemberFintan2-Oct-09 9:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web01 | 2.8.140916.1 | Last Updated 6 Oct 2009
Article Copyright 2009 by Aran Mulholland
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid