0

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?

Franek Stark
  • 37
  • 1
  • 7

1 Answers1

1

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.

aepot
  • 4,558
  • 2
  • 12
  • 24