How to start text on the left

I have an editable ComboBox, and when the text is added too long, it looks something like this:

enter image description here

How can I make a text box start at the beginning of a line?

TextBox txt = sender as TextBox; txt.Text = "[Children]"; <Style TargetType="TextBox"> <Setter Property="VerticalAlignment" Value="Center"/> <Setter Property="BorderBrush" Value="Silver"/> <Setter Property="BorderThickness" Value="1"/> <Setter Property="SnapsToDevicePixels" Value="True"/> <Setter Property="OverridesDefaultStyle" Value="True"/> <Setter Property="FocusVisualStyle" Value="{x:Null}"/> <Setter Property="AllowDrop" Value="true"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type TextBoxBase}"> <Border Name="Border" Padding="1" Background="#FFFFFF" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" > <ScrollViewer Margin="0" x:Name="PART_ContentHost"/> </Border> <ControlTemplate.Triggers> <Trigger Property="IsEnabled" Value="False"> <Setter TargetName="Border" Property="Background" Value="#EEEEEE"/> <Setter TargetName="Border" Property="BorderBrush" Value="#EEEEEE"/> <Setter Property="Foreground" Value="#888888"/> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style> 

ComboBox:

 ComboBox cmbValue1 = new ComboBox(); cmbValue1.IsTextSearchEnabled = false; cmbValue1.IsEditable = true; cmbValue1.Width = 70; TextBox txtEdit = (TextBox)((sender as ComboBox).Template.FindName("PART_EditableTextBox", (sender as ComboBox))); txtEdit.Tag = selection; 
+4
source share
1 answer

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://23.23.250.9/wpfresource.zip 
0
source

All Articles