Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF: Dropdown of a Combobox highlightes the text

Tags:

c#

combobox

wpf

When I type in the combobox I automatically opens enables the dropdown list

searchComboBox.IsDropDownOpen = true;

The problem here is - the text gets highlighted and the next keystrock overwrites the previous text.

How can I disable the text highlighting when ComboBox DropDown opens up?

like image 589
Panks Avatar asked Sep 17 '09 22:09

Panks


1 Answers

Better late than never and if some one else hit this proplem he might use this.

There is away todo this if you override combobox. First get handle on the textbox that is used in the template and register to selectionchanged event.

public override void OnApplyTemplate()
{
  base.OnApplyTemplate();

  var element = GetTemplateChild("PART_EditableTextBox");
  if (element != null)
  {
    var textBox = (TextBox)element;
    textBox.SelectionChanged += OnDropSelectionChanged;
  }
}

private void OnDropSelectionChanged(object sender, RoutedEventArgs e)
{
    // Your code
}

Then in the event handler you can set the selection again like you want it to be. In my case I was calling IsDropDownOpen in code. Saved selection there then put it back in the event handler. Ugly but did the trick.

like image 172
Einar Guðsteinsson Avatar answered Sep 21 '22 18:09

Einar Guðsteinsson