Set cursor / cursor position to end of WPF text field with string value

I am trying to set the cursor / cursor position to the end of a string value in a WPF text box when I open my window for the first time. I use FocusManager to adjust the focus on my text box when my window opens.

Nothing seems to work. Any ideas?

Notice that I am using the MVVM pattern, and I only included the XAML part from my code.

<Window FocusManager.FocusedElement="{Binding ElementName=NumberOfDigits}" Height="400" Width="800"> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition/> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition/> <RowDefinition/> </Grid.RowDefinitions> <TextBox Grid.Column="0" Grid.Row="0" x:Name="NumberOfDigits" IsReadOnly="{Binding Path=IsRunning, Mode=TwoWay}" VerticalContentAlignment="Center" Text="{Binding Path=Digits, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/> <Button Grid.Column="0" Grid.Row="1" Margin="10,0,10,0" IsDefault="True" Content="Start" Command="{Binding StartCommand}"/> </Grid> </Window> 
+52
wpf cursor caret cursor-position textbox
May 22 '10 at 16:49
source share
5 answers

You can set the caret position using the CaretIndex property of the TextBox . Please keep in mind that this is not a DependencyProperty . However, you can still install it in XAML as follows:

 <TextBox Text="123" CaretIndex="{x:Static System:Int32.MaxValue}" /> 

Remember to set the CaretIndex property after Text , otherwise it will not work. This way, it probably won't work if you bind to Text , as in your example. In this case, just use the code this way.

 NumberOfDigits.CaretIndex = NumberOfDigits.Text.Length; 
+72
May 23 '10 at 18:52
source share

You can also create Behavior, which, while maintaining the code, has the advantage of being reusable.

An example of a simple behavior class using a text field focus event:

 class PutCursorAtEndTextBoxBehavior: Behavior<UIElement> { private TextBox _textBox; protected override void OnAttached() { base.OnAttached(); _textBox = AssociatedObject as TextBox; if (_textBox == null) { return; } _textBox.GotFocus += TextBoxGotFocus; } protected override void OnDetaching() { if (_textBox == null) { return; } _textBox.GotFocus -= TextBoxGotFocus; base.OnDetaching(); } private void TextBoxGotFocus(object sender, RoutedEventArgs routedEventArgs) { _textBox.CaretIndex = _textBox.Text.Length; } } 

Then in your XAML you attach the behavior like this:

  <TextBox x:Name="MyTextBox" Text="{Binding Value}"> <i:Interaction.Behaviors> <behaviors:PutCursorAtEndTextBoxBehavior/> </i:Interaction.Behaviors> </TextBox> 
+16
Aug 21 '13 at 20:04 on
source share

If your text box (WinForms) is multi-line with a vertical scrollbar, you can try the following:

 textbox1.Select(textbox1.Text.Length-1, 1); textbox1.ScrollToCaret(); 

Note. In WPF.ScrollToCaret () is not a member of the TextBox.

+2
May 27 '16 at 11:43
source share

In the case of a multi-line cursor, setting a TextBox not enough. Try the following:

 NumberOfDigits.ScrollToEnd(); 
+1
Sep 26 '16 at 11:41
source share

It worked for me. I also use the MVVM pattern. However, my goal of using MMVM is to make unit testing possible and to simplify updating my interface (loosely coupled). I don’t see me testing the cursor position, so I don’t mind resorting to the code behind this simple task.

  public ExpeditingLogView() { InitializeComponent(); this.Loaded += (sender, args) => { Description.CaretIndex = Description.Text.Length; Description.ScrollToEnd(); Description.Focus(); }; } 
+1
Jun 09 '17 at 2:04 on
source share



All Articles