public override void OnApplyTemplate()
 {
     base.OnApplyTemplate();
     _upperLoadingBar = GetTemplateChild("PART_UpperLoadingBar") as LoadingBar;
     _bottomLoadingBar = GetTemplateChild("PART_BottomLoadingBar") as LoadingBar;
     Utility.PerformOnDispatcher(Dispatcher, () => { SetDropDownLoadingIndicator(IsLoading); });
 }
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            _border = GetTemplateChild("PART_Border") as Border;
            _dropDown = GetTemplateChild("PART_DropDown") as Popup;
            _lbDropDown = GetTemplateChild("PART_LBDropDown") as ListBox;
            _arrowHost = GetTemplateChild("PART_ArrowHost") as Border;
            _borderDropDown = GetTemplateChild("PART_BorderDropDown") as Border;
            _upperLoadingBar = GetTemplateChild("PART_UpperLoadingBar") as LoadingBar;
            _bottomLoadingBar = GetTemplateChild("PART_BottomLoadingBar") as LoadingBar;

            SetDropDownLoadingIndicator();
            _lbDropDown.ItemTemplate = DropDownItemTemplate;
            var style = new Style(typeof(ListBoxItem), (Style)FindResource("lbAutocompleteTextBoxDropDownItemStyle"));
            style.Setters.Add(new EventSetter(Mouse.PreviewMouseUpEvent, new MouseButtonEventHandler(lbDropDownItem_PreviewMouseButtonUp)));
            style.Setters.Add(new EventSetter(Mouse.MouseMoveEvent, new MouseEventHandler(lbDropDownItem_MouseMove)));
            _lbDropDown.ItemContainerStyle = style;
            _border.CornerRadius = CornerRadius;

            _lbDropDownItemMouseMoves
                .DistinctUntilChanged(x => x.EventArgs.GetPosition(this))
                .Subscribe(x => _lbDropDown.SelectedItem = x.Sender.Content);
            var previewKeyDowns = Observable.FromEventPattern<KeyEventHandler, KeyEventArgs>(
                h => PreviewKeyDown += h, h => PreviewKeyDown -= h);
            previewKeyDowns.Where(x => x.EventArgs.Key == Key.Enter)
                .Subscribe(x => AcceptItem(x.EventArgs));
            var previewKeyDownsKeyDowns = previewKeyDowns.Where(x => x.EventArgs.Key == Key.Down);
            previewKeyDownsKeyDowns.Where(_ => _dropDown.IsOpen).Subscribe(_ => MoveDown());
            previewKeyDownsKeyDowns.Where(_ => !_dropDown.IsOpen).Subscribe(_ => OpenDropDownByArrowButtonOrKey());
            previewKeyDowns.Where(x => x.EventArgs.Key == Key.Up).Subscribe(_ => MoveUp());
            previewKeyDowns.Where(x => x.EventArgs.Key == Key.Escape).Subscribe(_ => CancelOrIgnore());
            previewKeyDowns.Where(x => x.EventArgs.Key == Key.PageUp).Subscribe(_ => MovePageUp());
            previewKeyDowns.Where(x => x.EventArgs.Key == Key.PageDown).Subscribe(_ => MovePageDown());

            var downs = _arrowHost.Events().MouseLeftButtonDown
                .Select(_ => Mouse.Captured)
                .Do(_ => Mouse.Capture(_arrowHost));
            var ups = _arrowHost.Events().MouseLeftButtonUp;
            downs
                .GroupJoin(ups, d => downs, u => Observable.Return(Unit.Default),
                    (d, us) => ups.Take(1).Select(_ => d))
                .Merge()
                .Do(RestoreMouseCapture)
                .Subscribe(_ => OpenDropDownByArrowButtonOrKey());

            _dropDown.Closed += _dropDown_Closed;
            _dropDown.Opened += dropDown_Opened;
            LostFocus += AutocompleteTextBox_LostFocus;
        }