I have combobox in WPF application that when the user clicks on it, it selects all the text. How can I change the behavior of this to when the user clicks it just set the typing cursor like a normal textbox?
I think he wants to have it editable, but he does not want the text in the `TextBox` (within the EditTemplate) to be selected when he clicks on it, but he wants the cursor to be positioned before/behind the character that he clicked.
gehho
2010-09-08 12:56:39
Thanks gehho, this is what I need! Any idea!
Mark
2010-09-08 13:22:17
A:
According to Reflector, the ComboBox
code contains this:
private static void OnGotFocus(object sender, RoutedEventArgs e)
{
ComboBox box = (ComboBox) sender;
if ((!e.Handled && box.IsEditable) && (box.EditableTextBoxSite != null))
{
if (e.OriginalSource == box)
{
box.EditableTextBoxSite.Focus();
e.Handled = true;
}
else if (e.OriginalSource == box.EditableTextBoxSite)
{
box.EditableTextBoxSite.SelectAll(); // <==
}
}
}
This method is registered for the GotFocus
event in the static constructor using the EventManager
:
EventManager.RegisterClassHandler(typeof(ComboBox), UIElement.GotFocusEvent, new RoutedEventHandler(ComboBox.OnGotFocus));
So, I think you can only change that behavior by deriving a custom control from ComboBox
and override this event registration by your own method which replaces the call to SelectAll()
with another method which sets the caret to the correct position. However, I do not know how to set the caret to the click position. You might have to use Reflector on the TextBox to find that...
gehho
2010-09-08 14:03:26