Search code examples
c#wpfxamldata-bindingdelay

Source not getting updated when user types too fast


I am trying to create a small delay on a textbox UpdateSourceTrigger, to insure that the user in done typing.

<TextBox 
  Text="{Binding SearchEngineCompassLogView.IdSearch, 
  Mode=TwoWay, 
  Source={StaticResource CompassLogView}, 
  UpdateSourceTrigger=PropertyChanged, Delay=2000}" />

The problem is that if the user Types really fast, the source doesn't get updated. I have set the Delay to 2000 sec, so the problem is more obvious.

the property:

public string IdSearch {
    get { return _idSearch; }
    set {
        if (_idSearch != value && value != null) {
            _idSearch = value;
            NotifyPropertyChanged();
            SearchForID(_idSearch);
        }
    }
}

The problem is not NotifyPropertyChanged();


Solution

  • I can´t reproduce the behavior you described. I also think that you have made an error in the binding definition of the Text property (If its the case you should get binding errors in the Output Window of Visual Studio during the debugging session).

    I think it should be

    Text="{Binding IdSearch,
    

    But i can´t tell cause i don´t know your class structure.

    I have made a small example to demonstrate that the delay and the update of the property works as expected. When you set a BreakPoint in the setter of the property IdSearch and run the example, you will see that the setter is called after the specified delay time. It doesn´t matter how fast you type in the words.

    XAML:

    <Window x:Class="WpfApplication2.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:local="clr-namespace:WpfApplication2"
            Title="MainWindow"
            Width="525"
            Height="350">
        <Window.Resources>
            <local:Dummy x:Key="CompassLogView" />
        </Window.Resources>
        <Grid>
            <TextBox Text="{Binding IdSearch, Mode=TwoWay, Source={StaticResource CompassLogView}, UpdateSourceTrigger=PropertyChanged, Delay=2000}" />
        </Grid>
    </Window>
    

    C#:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
    
    namespace WpfApplication2 {
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window {
            public MainWindow() {
                InitializeComponent();
            }
        }
    
        public class Dummy {
            private string _idSearch;
            public string IdSearch {
                get { return _idSearch; }
                set {
                    if (_idSearch != value && value != null) {
                        _idSearch = value;
                        //NotifyPropertyChanged();
                        //SearchForID(_idSearch);
                    }
                }
            }
        }
    }