Scrolling to the end of a single line WPF TextBox

前端 未结 3 1404
梦谈多话
梦谈多话 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:42

    It is true - setting the caret property from code doesn't affect the view; and the caret can go outside the visible part.

    kb_target_box.Focus(); // just for sure
    Rect rect = kb_target_box.GetRectFromCharacterIndex(kb_target_box.CaretIndex);
    kb_target_box.ScrollToHorizontalOffset(Math.Max((kb_target_box.HorizontalOffset + rect.Left - (kb_target_box.ActualWidth - 40)), 0.0));
    

    h-scroll will follow the caret after it comes closer than 40 to the right TextBox border.

提交回复
热议问题