예제 #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);
        }
예제 #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;
        }