/// <summary>
 /// Like FocusNextCompletion but focuses the previous completion.
 /// </summary>
 /// <param name="stackPanel"></param>
 public static void FocusPreviousCompletion(StackPanel stackPanel, TextBox textBox)
 {
     for (int i = 0; i < stackPanel.Children.Count; i++)
     {
         TextBlock child = (stackPanel.Children[i] as TextBlock);
         if (child.IsFocused && stackPanel.Children.Count > 1 && i - 1 > -1)
         {
             AutoCompleteResult toFocus      = (stackPanel.Children[i] as AutoCompleteResult);
             AutoCompleteResult takeTextFrom = (stackPanel.Children[i - 1] as AutoCompleteResult);
             toFocus.Focus();
             textBox.Text = (takeTextFrom.Text);
             return;
         }
     }
 }
 /// <summary>
 /// Focuses the completion below the current completion, or the first completion if none is selected.
 /// </summary>
 /// <param name="stackPanel">The StackPanel holding the autocompletions</param>
 /// <param name="textBox">The destination text box that the autocomplete will complete.</param>
 public static void FocusNextCompletion(StackPanel stackPanel, TextBox textBox)
 {
     for (int i = 0; i < stackPanel.Children.Count; i++)
     {
         AutoCompleteResult child = (stackPanel.Children[i] as AutoCompleteResult);
         if (child.IsFocused && stackPanel.Children.Count > 1 && i + 1 < stackPanel.Children.Count)
         {
             textBox.Text = (stackPanel.Children[i + 1] as AutoCompleteResult).Text;
             return;
         }
     }
     if (stackPanel.Children.Count > 0)
     {
         AutoCompleteResult destination = stackPanel.Children[0] as AutoCompleteResult;
         destination.Focus();
         textBox.Text = destination.Text;
     }
 }
Ejemplo n.º 3
0
        public static AutoCompleteResult Generate(string text, StackPanel stackPanel, TextBox textBox)
        {
            AutoCompleteResult final = new AutoCompleteResult
            {

                // Add the text   
                Text = text,
                // A little style...   
                Margin = new Thickness(2, 3, 2, 3),
                Cursor = Cursors.Hand,
                Focusable = true
            };

            // Mouse events   
            final.MouseLeftButtonUp += (sender, e) =>
            {
                textBox.Text = (sender as TextBlock).Text;
                AutoComplete.Close(stackPanel);
            };

            final.MouseEnter += (sender, e) =>
            {
                //TextBlock b = sender as TextBlock;
                //b.Background = System.Windows.Media.Brushes.LightSkyBlue;
                final.IsSelected = true;
            };

            final.MouseLeave += (sender, e) =>
            {
                final.IsSelected = false;
            };

            final.LostFocus += (sender, e) =>
            {
                final.IsSelected = false;
            };

            final.GotFocus += (sender, e) =>
            {
                final.IsSelected = true;
            };

            final.PreviewKeyDown += (sender, e) =>
            {
                switch (e.Key)
                {
                    case Key.Down:
                        AutoComplete.FocusNextCompletion(stackPanel, textBox);
                        return;
                    case Key.Up:
                        AutoComplete.FocusPreviousCompletion(stackPanel, textBox);
                        return;
                    case Key.Escape:
                        AutoComplete.Close(stackPanel);
                        return;
                    default:
                        return;
                }
            };

            //final.LostFocus += (sender, e) =>
            //{
            //    final.IsSelected = false;
            //};
            return final;
        }
 /// <summary>
 /// Adds item to the stack panel.
 /// </summary>
 /// <param name="text"></param>
 /// <param name="stackPanel"></param>
 /// <param name="textBox"></param>
 public static void AddItemToStack(string text, StackPanel stackPanel, TextBox textBox)
 {
     stackPanel.Children.Add(AutoCompleteResult.Generate(text, stackPanel, textBox));
 }