Scrolling to the end of a single line WPF TextBox

前端 未结 3 1406
梦谈多话
梦谈多话 2020-12-18 03:20

This seems like such a simple thing but I just can\'t get it to work.

I have a single line text box that has a lot of text. What I want to happen is that whenever t

3条回答
  •  眼角桃花
    2020-12-18 03:53

    You should be able to do it using these members:

    • TextBoxBase.ScrollToEnd
    • TextBox.CaretIndex

    EDIT: not sure why ScrollToEnd isn't working... Anyway, this code works:

    textBox.CaretIndex = textBox.Text.Length;
    var rect = textBox.GetRectFromCharacterIndex(textBox.CaretIndex);
    textBox.ScrollToHorizontalOffset(rect.Right);
    

提交回复
热议问题