/// <summary>
 /// When we assign the target control we set up event handlers.
 /// </summary>
 /// <param name="d"></param>
 /// <param name="e"></param>
 private static void TargetControl_Changed(DependencyObject d, DependencyPropertyChangedEventArgs e)
 {
     if (e.OldValue != e.NewValue)
     {
         TextBoxAutoComplete me   = d as TextBoxAutoComplete;
         TextBox             oldv = e.OldValue as TextBox;
         TextBox             newv = e.NewValue as TextBox;
         if (oldv != null)
         {
             oldv.LostFocus      -= new RoutedEventHandler(me.TargetControl_LostFocus);
             oldv.GotFocus       -= new RoutedEventHandler(me.TargetControl_GotFocus);
             oldv.KeyUp          -= new KeyEventHandler(me.TargetControl_KeyUp);
             oldv.PreviewKeyUp   -= new KeyEventHandler(me.TargetControl_PreviewKeyUp);
             oldv.PreviewKeyDown -= new KeyEventHandler(me.TargetControl_PreviewKeyDown);
         }
         if (newv != null)
         {
             me.popup.PlacementTarget = newv;
             newv.LostFocus          += new RoutedEventHandler(me.TargetControl_LostFocus);
             newv.GotFocus           += new RoutedEventHandler(me.TargetControl_GotFocus);
             newv.KeyUp          += new KeyEventHandler(me.TargetControl_KeyUp);
             newv.PreviewKeyUp   += new KeyEventHandler(me.TargetControl_PreviewKeyUp);
             newv.PreviewKeyDown += new KeyEventHandler(me.TargetControl_PreviewKeyDown);
         }
     }
 }
        private static void SelectedListBoxValue_Changed(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            TextBoxAutoComplete me = d as TextBoxAutoComplete;

            if (e.NewValue != null)
            {
                if (!me.popup.IsOpen)
                {
                    var result = me.odp.SearchByKey(e.NewValue);
                    result.Results.ToList().ForEach(item => me.searchResults.Add(item));
                    me.SetTextAndHide();
                }
                else
                {
                    me.TargetControl.Text = String.IsNullOrEmpty(me.DisplayMemberPath) ?
                                            me.listBox.SelectedItem.ToString() :
                                            me.listBox.SelectedItem.GetType().GetProperty(
                        me.DisplayMemberPath).GetValue(me.listBox.SelectedItem, null).ToString();
                }
            }
        }
        private static void SearchDataProvider_Changed(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            TextBoxAutoComplete me = d as TextBoxAutoComplete;

            me.odp = e.NewValue as ISearchDataProvider;
        }