0
votes

Im devloping an Application which uses a lot auf Textboxes. If they got focus the Caret is at the first position of the Textbox. Even if already text exists. Im searching for a soloution to set the caret at the end of the text FOR ALL Texbox, when they got focus.

I know, that I can handle the GotFocus()-Event and then set the position manually. But is there a smarter soloution?

1

1 Answers

1
votes

The easiest way is using Event handler as you said.

private void TextBox_GotFocus(object sender, RoutedEventArgs e)
{
    TextBox textBox = sender as TextBox;
    textBox.CaretIndex = textBox.Text.Length;
}

Then assign this Event handler to each TextBox.

The CaretIndex in generic TextBox is not a property. Thus the smarter solution need at least creation of a custom Control inherited from TextBox. You may make CaretIndex a property this way. Then just use Style.Triggers in xaml with Property IsFocused.