Custom UpdateSourceTrigger with delay?

3.6k views Asked by At

I'm looking to create a custom version of UpdateSourceTrigger that I can use with my binding. I don't know if this is possible, or if instead, I'd need to just create my own binding class. What I'm looking for is, instead of LostFocus or PropertyChanged, have something where it will update the source after some specified time limit.

I found this, but I don't know if there's a better way (one of the comments mentioned some memory leaks with the implementation).

Any ideas?

3

There are 3 answers

0
Kent Boogaart On BEST ANSWER

I wouldn't bother doing this at the binding level, but would instead manifest it in my view model. When the property changes, restart a DispatcherTimer. When the timer expires, kick off your logic. It's that simple.

0
Oliver Weichhold On

This can be easily implemented using Reactive Extensions's Throttle() method in conjunction with an observable property.

public class ObservablePropertyBacking<T> : IObservable<T>
{
  private readonly Subject<T> _innerObservable = new Subject<T>();

  private T _value;

  public T Value
  {
    get { return _value; }
    set
    {
      _value = value;
      _innerObservable.OnNext(value);
    }
  }

  #region IObservable<T> Members

  public IDisposable Subscribe(IObserver<T> observer)
  {
    return _innerObservable
      .DistinctUntilChanged()
      .AsObservable()
      .Subscribe(observer);
  }

  #endregion
}

Used like this:

// wire query observable
var queryActual = new ObservablePropertyBacking<string>();
queryActual.Throttle(TimeSpan.FromMilliseconds(300)).Subscribe(DoSearch);

Implement property:

string query;

public string Query
{
  get { return query; }
  set
  {
    queryActual.Value = value;
  }
}
2
rfcdejong On

I just noticed that WPF 4.5 has a Delay Property, see for more information this link

http://www.shujaat.net/2011/12/wpf-45-developers-preview-delay-binding.html