在readonly TextBox中移动到文本的末尾

时间:2013-08-15 10:43:30

标签: c# wpf xaml textbox readonly

我目前在我的WPF应用程序中有一个只读的TextBox:

<TextBox x:Name="TextBox_CurrentDirectory" IsReadOnly="True"></TextBox>

文本在Code Behind中更新:

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    var app = Application.Current as App;
    TextBox_CurrentDirectory.Text = app.ActiveDirectory;
    //Show the end of the text here
}

我有办法以编程方式显示文本的结尾吗?如果TextBox中的文本比TextBox长,则它只显示开始并被截断。我希望能够显示文本的结尾。

我尝试使用

TextBox_CurrentDirectory.CaretIndex = TextBox_CurrentDirectory.Text.Length;

但没有任何反应。

1 个答案:

答案 0 :(得分:4)

您需要先设置TextBox焦点,然后再设置CaretIndex

TextBox_CurrentDirectory.Text = app.ActiveDirectory;
TextBox_CurrentDirectory.Focus();
TextBox_CurrentDirectory.CaretIndex = TextBox_CurrentDirectory.Text.Length;
相关问题