You need to set the start of the selection to 0 to make it โleft-alignedโ after the user enters something.
Do this through the TextBoxBase.PreviewLostKeyboardFocus event - here is some XAML:
<ComboBox Name="ComboBox1" IsEditable="True" TextBoxBase.PreviewLostKeyboardFocus="TextBox_PreviewLostKeyboardFocus_1">
And the event itself:
private void TextBox_PreviewLostKeyboardFocus_1(object sender, KeyboardFocusChangedEventArgs e) { TextBox txtEdit = (TextBox)((sender as ComboBox).Template.FindName("PART_EditableTextBox", (sender as ComboBox))); txtEdit.SelectionStart = 0; }
I think this should give enough so that you can adapt to your application. I tested in an empty WPF application and it works whether you press TAB or click on another part of the user interface with the mouse.
Edit:
Here's how to add an event to the code:
Not sure how your application is structured, but again this works for me:
private void Window_Initialized_1(object sender, EventArgs e) { ComboBox cmbValue1 = new ComboBox(); cmbValue1.IsTextSearchEnabled = false; cmbValue1.IsEditable = true; cmbValue1.Width = 70; cmbValue1.PreviewLostKeyboardFocus += TextBox_PreviewLostKeyboardFocus_1; this.MyCanvas.Children.Add(cmbValue1); }
(Using the same event handler that I wrote above in this answer)
Edit:
Here is a link to a working draft: Note - it works if the text in the edited ComboBox is not selected when you lose focus (i.e. after entering something or just deselecting). I will try to fix it.
http:
source share