Click here to Skip to main content
15,897,891 members
Articles / Desktop Programming / WPF

A Reusable WPF Autocomplete TextBox (Part 2)

Rate me:
Please Sign up or sign in to vote.
4.90/5 (6 votes)
24 Nov 2011CPOL6 min read 51.7K   3.2K   38  
This article is about a control that can be used to auto-omplete folders that have been typed into a textbox. It shows just how versatile binding and theming in WPF really is.
namespace Aviad.WPF.Controls
{
  using System.Windows;
  using System.Windows.Controls;
  using System.Windows.Input;

  public static class ListBoxItemBehavior
  {
    #region fields
    // Using a DependencyProperty as the backing store for SelectOnMouseOver.
    // This enables animation, styling, binding, etc...
    public static readonly DependencyProperty SelectOnMouseOverProperty =
        DependencyProperty.RegisterAttached("SelectOnMouseOver",
                                             typeof(bool),
                                             typeof(ListBoxItemBehavior),
                                             new UIPropertyMetadata(false, OnSelectOnMouseOverChanged));
    #endregion fields

    #region methods
    public static bool GetSelectOnMouseOver(DependencyObject obj)
    {
      return (bool)obj.GetValue(SelectOnMouseOverProperty);
    }

    public static void SetSelectOnMouseOver(DependencyObject obj, bool value)
    {
      obj.SetValue(SelectOnMouseOverProperty, value);
    }

    private static void OnSelectOnMouseOverChanged(DependencyObject d,
                                           DependencyPropertyChangedEventArgs e)
    {
      ListBoxItem lbi = d as ListBoxItem;
      if (lbi == null) return;

      bool bNew = (bool)e.NewValue, bOld = (bool)e.OldValue;

      if (bNew == bOld) return;

      if (bNew)
        lbi.MouseEnter += new MouseEventHandler(lbi_MouseEnter);
      else
        lbi.MouseEnter -= lbi_MouseEnter;
    }

    private static void lbi_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
    {
      ListBoxItem lbi = (ListBoxItem)sender;
      lbi.IsSelected = true;
      var listBox = ListBox.ItemsControlFromItemContainer(lbi);

      FrameworkElement focusedElement =
                (FrameworkElement)FocusManager.GetFocusedElement(FocusManager.GetFocusScope(listBox));

      if (focusedElement != null && focusedElement.IsDescendantOf(listBox))
        lbi.Focus();
    }
    #endregion fields
  }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Germany Germany
The Windows Presentation Foundation (WPF) and C# are among my favorites and so I developed Edi

and a few other projects on GitHub. I am normally an algorithms and structure type but WPF has such interesting UI sides that I cannot help myself but get into it.

https://de.linkedin.com/in/dirkbahle

Comments and Discussions