Esempio n. 1
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)
                    {
                        if (parent is AutoCompleteDropDownAdorner oldAdorner)
                        {
                            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);
        }
Esempio n. 2
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);
 }
Esempio n. 3
0
 /// <summary>
 /// Disconnects a visual child element
 /// </summary>
 /// <param name="ui"></param>
 public void DisconnectVisualChild(AutoComplete ui) => RemoveVisualChild(ui);
Esempio 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);
Esempio n. 5
0
 /// <summary>
 /// Disconnects a visual child element
 /// </summary>
 /// <param name="ui"></param>
 public void DisconnectVisualChild(AutoComplete ui)
 {
     RemoveVisualChild(ui);
 }
Esempio n. 6
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);
 }
Esempio n. 7
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;
        }
Esempio n. 8
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);
 }