Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set the caret/cursor position to the end of the string value WPF textbox

I am try to set the caret/cursor position to the end of the string value in my WPF textbox when I open my window for the first time. I use the FocusManager to set the focus on my textbox when my window opens.

Nothing seems to work. Any ideas?

Note, I am using the MVVM pattern, and I included only a portion of the XAML 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> 
like image 763
Zamboni Avatar asked May 22 '10 16:05

Zamboni


People also ask

How to change cursor position in TextBox?

To position the cursor at the end of the contents of a TextBox control, call the Select method and specify the selection start position equal to the length of the text content, and a selection length of 0.

How to set cursor focus on TextBox in c#?

Select(); textBox1. Select( 0, 0 ); ... textBox1. Select(0, 0); will select character 0 to character 0 in the textbox1.

What is TextBox caret?

The CaretElement is an internal sealed class and not possible to customize through a data template for example. At least, the caret brush is possible to change. <TextBox Text="This is some random text" CaretBrush="Blue" /> If you want to have a linear gradient on the caret brush, this can be done.

What is TextBox in WPF?

The TextBox class enables you to display or edit unformatted text. A common use of a TextBox is editing unformatted text in a form. For example, a form asking for the user's name, phone number, etc would use TextBox controls for text input.


2 Answers

You can set the caret position using CaretIndex property of a TextBox. Please bear in mind that this is not a DependencyProperty. Nevertheless, you may still set it in XAML like this:

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

Please remember to set CaretIndex after Text property or else it will not work. Thus it probably won't work if you bind to Text like in your example. In that case, simply use code-behind like this.

NumberOfDigits.CaretIndex = NumberOfDigits.Text.Length; 
like image 176
wpfwannabe Avatar answered Sep 21 '22 00:09

wpfwannabe


You may also create a Behavior, which, while still being code-behind, has the advantage of being reusable.

Example of a simple behavior class, using the focus event of the textbox:

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 so:

    <TextBox x:Name="MyTextBox" Text="{Binding Value}">         <i:Interaction.Behaviors>             <behaviors:PutCursorAtEndTextBoxBehavior/>         </i:Interaction.Behaviors>     </TextBox> 
like image 21
Louis Avatar answered Sep 17 '22 00:09

Louis