From a0f22ed522c30e3b51984302f7923fcbf657bcd1 Mon Sep 17 00:00:00 2001 From: Daniel Grunwald Date: Wed, 16 Feb 2011 14:56:15 +0100 Subject: [PATCH] Add delay to SearchBox. --- ILSpy/Controls/SearchBox.cs | 29 +++++++++++++++++++++++++++++ ILSpy/MainWindow.xaml | 2 +- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/ILSpy/Controls/SearchBox.cs b/ILSpy/Controls/SearchBox.cs index 4a27b25ef..2926de8f4 100644 --- a/ILSpy/Controls/SearchBox.cs +++ b/ILSpy/Controls/SearchBox.cs @@ -21,6 +21,7 @@ using System.Windows; using System.Windows.Controls; using System.Windows.Input; using System.Windows.Media; +using System.Windows.Threading; namespace ICSharpCode.ILSpy.Controls { @@ -40,6 +41,10 @@ namespace ICSharpCode.ILSpy.Controls public static DependencyProperty HasTextProperty = DependencyProperty.Register("HasText", typeof(bool), typeof(SearchBox)); + public static readonly DependencyProperty UpdateDelayProperty = + DependencyProperty.Register("UpdateDelay", typeof(TimeSpan), typeof(SearchBox), + new FrameworkPropertyMetadata(TimeSpan.FromMilliseconds(200))); + #endregion #region Public Properties @@ -59,6 +64,11 @@ namespace ICSharpCode.ILSpy.Controls private set { SetValue(HasTextProperty, value); } } + public TimeSpan UpdateDelay { + get { return (TimeSpan)GetValue(UpdateDelayProperty); } + set { SetValue(UpdateDelayProperty, value); } + } + #endregion #region Handlers @@ -72,10 +82,29 @@ namespace ICSharpCode.ILSpy.Controls #region Overrides + DispatcherTimer timer; + protected override void OnTextChanged(TextChangedEventArgs e) { base.OnTextChanged(e); HasText = this.Text.Length > 0; + if (timer == null) { + timer = new DispatcherTimer(); + timer.Tick += timer_Tick; + } + timer.Stop(); + timer.Interval = this.UpdateDelay; + timer.Start(); + } + + void timer_Tick(object sender, EventArgs e) + { + timer.Stop(); + timer = null; + var textBinding = GetBindingExpression(TextProperty); + if (textBinding != null) { + textBinding.UpdateSource(); + } } protected override void OnLostFocus(RoutedEventArgs e) diff --git a/ILSpy/MainWindow.xaml b/ILSpy/MainWindow.xaml index 61c809bb2..f53536e23 100644 --- a/ILSpy/MainWindow.xaml +++ b/ILSpy/MainWindow.xaml @@ -131,7 +131,7 @@ + Text="{Binding FilterSettings.SearchTerm}" />