Exemplo n.º 1
0
 /// <summary>
 /// Disconnects a visual child element
 /// </summary>
 /// <param name="ui"></param>
 public void DisconnectVisualChild(AutoComplete ui)
 {
     RemoveVisualChild(ui);
 }
Exemplo n.º 2
0
        /// <summary>Returns an existing reference to the UI used for the drop-down (or creates a new one)</summary>
        /// <param name="attachedTo">Object the UI is to be attached to</param>
        /// <returns>UI element</returns>
        private static AutoComplete GetAutoCompleteUIElement(UIElement attachedTo)
        {
            var dropDownUI = GetAutoCompleteUI(attachedTo);
            if (dropDownUI == null)
            {
                dropDownUI = new AutoComplete();

                attachedTo.GotFocus += (sender, e) =>
                    {
                        var adornerLayer = AdornerLayer.GetAdornerLayer(attachedTo);
                        if (adornerLayer == null) return;
                        var currentAdorners = adornerLayer.GetAdorners(attachedTo);
                        var adornerFound = false;
                        if (currentAdorners != null)
                            if (currentAdorners.OfType<AutoCompleteDropDownAdorner>().Any())
                                adornerFound = true;

                        if (dropDownUI._adornerLoaded && adornerFound) return;
                        var parent = VisualTreeHelper.GetParent(dropDownUI);
                        if (parent != null)
                        {
                            var oldAdorner = parent as AutoCompleteDropDownAdorner;
                            if (oldAdorner != null)
                                oldAdorner.DisconnectVisualChild(dropDownUI);
                        }
                        var adorner = new AutoCompleteDropDownAdorner(attachedTo, dropDownUI);
                        var localAdorner = adorner;
                        adornerLayer.Add(adorner);
                        dropDownUI._adornerLoaded = true;

                        if (dropDownUI.Items.Count > 0) dropDownUI.Visibility = Visibility.Visible;
                        localAdorner.InvalidateMeasure();
                        localAdorner.InvalidateArrange();
                    };
                attachedTo.PreviewKeyDown += (sender, e) =>
                    {
                        switch (e.Key)
                        {
                            case Key.Up:
                            case Key.Down:
                                {
                                    var index = dropDownUI.SelectedIndex;
                                    switch (e.Key)
                                    {
                                        case Key.Down:
                                            index++;
                                            if (index >= dropDownUI.Items.Count)
                                                index = dropDownUI.Items.Count - 1;
                                            break;
                                        case Key.Up:
                                            index--;
                                            if (index < 0)
                                                index = 0;
                                            break;
                                    }
                                    dropDownUI.SelectedIndex = index;
                                    dropDownUI.ScrollIntoView(dropDownUI.SelectedItem);
                                    e.Handled = true;
                                }
                                break;
                            case Key.Enter:
                                {
                                    if (dropDownUI.SelectedIndex > -1)
                                        SetAutoCompleteSelectedItem(attachedTo, dropDownUI.SelectedItem);
                                    dropDownUI.Visibility = Visibility.Collapsed;
                                    dropDownUI._uiEligibleToBeVisible = false;
                                    attachedTo.Focus();
                                    var text = attachedTo as TextBox;
                                    if (text != null)
                                        text.SelectionStart = text.Text.Length;
                                }
                                break;
                        }
                    };
                attachedTo.KeyUp += (sender, e) =>
                    {
                        if (e.Key != Key.Enter && dropDownUI.Visibility == Visibility.Collapsed)
                            if (dropDownUI.Items.Count > 0)
                                dropDownUI.Visibility = Visibility.Visible;
                            else
                                dropDownUI._uiEligibleToBeVisible = true;
                        if (dropDownUI.Visibility == Visibility.Visible && dropDownUI.Items.Count == 0) dropDownUI.Visibility = Visibility.Collapsed;
                    };
                dropDownUI.PreviewKeyDown += (sender, e) =>
                    {
                        if (e.Key != Key.Enter) return;
                        if (dropDownUI.SelectedIndex > -1)
                            SetAutoCompleteSelectedItem(attachedTo, dropDownUI.SelectedItem);
                        dropDownUI.Visibility = Visibility.Collapsed;
                        dropDownUI._uiEligibleToBeVisible = false;
                    };
                dropDownUI.MouseLeftButtonUp += (sender, e) =>
                    {
                        if (dropDownUI.SelectedIndex <= -1) return;
                        SetAutoCompleteSelectedItem(attachedTo, dropDownUI.SelectedItem);
                        dropDownUI.Visibility = Visibility.Collapsed;
                        dropDownUI._uiEligibleToBeVisible = false;
                        e.Handled = true;
                        attachedTo.Focus();
                        var text = attachedTo as TextBox;
                        if (text != null)
                            text.SelectionStart = text.Text.Length;
                    };
                attachedTo.LostFocus += (sender, e) =>
                    {
                        if (!dropDownUI.IsKeyboardFocusWithin && !attachedTo.IsKeyboardFocusWithin)
                        {
                            dropDownUI.Visibility = Visibility.Collapsed;
                            dropDownUI._uiEligibleToBeVisible = false;
                        }
                    };
                dropDownUI.LostFocus += (sender, e) =>
                    {
                        if (!dropDownUI.IsKeyboardFocusWithin && !attachedTo.IsKeyboardFocusWithin)
                        {
                            dropDownUI.Visibility = Visibility.Collapsed;
                            dropDownUI._uiEligibleToBeVisible = false;
                        }
                    };

                SetAutoCompleteUI(attachedTo, dropDownUI);
            }
            return dropDownUI;
        }
Exemplo n.º 3
0
 /// <summary>Constructor</summary>
 /// <param name="adornedElement">Adorned element (typically a textbox)</param>
 /// <param name="ui">The UI that is to be used in the drop-down</param>
 public AutoCompleteDropDownAdorner(UIElement adornedElement, AutoComplete ui)
     : base(adornedElement)
 {
     _ui = ui;
     AddVisualChild(ui);
 }
Exemplo n.º 4
0
 /// <summary>Auto complete UI</summary>
 /// <param name="obj">Object to set the auto complete UI on</param>
 /// <param name="value">Value to set</param>
 public static void SetAutoCompleteUI(DependencyObject obj, AutoComplete value)
 {
     obj.SetValue(AutoCompleteUIProperty, value);
 }