将插入符号/光标位置设置为字符串值 WPF 文本框的末尾

当我第一次打开窗口时,我尝试将插入符号/光标的位置设置为 WPF 文本框中字符串值的 结束。当窗口打开时,我使用 FocusManager 设置文本框的焦点。

好像都不管用,有什么办法吗?

注意,我使用的是 MVVM 模式,并且只包含了代码中 XAML 的一部分。

<Window
FocusManager.FocusedElement="{Binding ElementName=NumberOfDigits}"
Height="400" Width="800">


<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>


<TextBox Grid.Column="0" Grid.Row="0"
x:Name="NumberOfDigits"
IsReadOnly="{Binding Path=IsRunning, Mode=TwoWay}"
VerticalContentAlignment="Center"
Text="{Binding Path=Digits, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
<Button Grid.Column="0" Grid.Row="1"
Margin="10,0,10,0"
IsDefault="True"
Content="Start"
Command="{Binding StartCommand}"/>
</Grid>
</Window>
96110 次浏览

You can set the caret position using CaretIndex property of a TextBox. Please bear in mind that this is not a DependencyProperty. Nevertheless, you may still set it in XAML like this:

<TextBox Text="123" CaretIndex="{x:Static System:Int32.MaxValue}" />

Please remember to set CaretIndex after Text property or else it will not work. Thus it probably won't work if you bind to Text like in your example. In that case, simply use code-behind like this.

NumberOfDigits.CaretIndex = NumberOfDigits.Text.Length;

You may also create a Behavior, which, while still being code-behind, has the advantage of being reusable.

Example of a simple behavior class, using the focus event of the textbox:

class PutCursorAtEndTextBoxBehavior: Behavior<UIElement>
{
private TextBox _textBox;


protected override void OnAttached()
{
base.OnAttached();


_textBox = AssociatedObject as TextBox;


if (_textBox == null)
{
return;
}
_textBox.GotFocus += TextBoxGotFocus;
}


protected override void OnDetaching()
{
if (_textBox == null)
{
return;
}
_textBox.GotFocus -= TextBoxGotFocus;


base.OnDetaching();
}


private void TextBoxGotFocus(object sender, RoutedEventArgs routedEventArgs)
{
_textBox.CaretIndex = _textBox.Text.Length;
}
}

Then, in your XAML, you attach the behavior like so:

    <TextBox x:Name="MyTextBox" Text="{Binding Value}">
<i:Interaction.Behaviors>
<behaviors:PutCursorAtEndTextBoxBehavior/>
</i:Interaction.Behaviors>
</TextBox>

In case of multiline TextBox setting cursor is not enough. Try this:

NumberOfDigits.ScrollToEnd();

This worked for me. I am also using the MVVM pattern. However, my purpose for using a MMVM is to make unit testing possible and to make it easier to update my UI (loosely coupled). I don't see myself unit testing the position of the cursor so I don't mind resorting to the code behind for this simple task.

    public ExpeditingLogView()
{
InitializeComponent();


this.Loaded += (sender, args) =>
{
Description.CaretIndex = Description.Text.Length;
Description.ScrollToEnd();
Description.Focus();
};
}

In WPF if line is long enough it is important also to scroll to the end of the line. So i'm using the following lines:

text_Box.Text = text;
text_Box.CaretIndex = text.Length;
text_Box.ScrollToHorizontalOffset(double.MaxValue);
// or you can use this - for me works also
// text_Box.ScrollToHorizontalOffset(text_Box.GetRectFromCharacterIndex(openFileDialog.FileName.Length).Right);

but read this caution (for me it's fine - probably already fixed): TextBox ScrollToHorizontalOffset will not scroll after text is long enough

None of the answers here worked for me. I'm using binding for the TextBox and needed to move the caret right after the window poped up. This did it for me:

public MyWindow()
{
InitializeComponent();


ContentRendered += (sender, args) =>
{
MyTextBox.CaretIndex = MyTextBox.Text.Length;
MyTextBox.ScrollToEnd(); // not necessary for single line texts
MyTextBox.Focus();
};
}

Similar to Ceranski answer. Instead of adding to the Loaded event we add to ContentRendered.

@Louis solution will not work if textbox used in template binding or any type of lazy bindings or lazy value assignments

So if the textbox used for example in Datagrid cell as a template that solution will need for tiny modification to work

and that is subscribing to text changed event

 class PutCursorAtEndTextBoxBehavior : Behavior<UIElement>
{
private TextBox _textBox;


protected override void OnAttached()
{
base.OnAttached();


_textBox = AssociatedObject as TextBox;


if (_textBox == null)
{
return;
}
_textBox.GotFocus += TextBoxGotFocus;
// to make it work with binding
_textBox.TextChanged += TextBoxGotFocus;
}


protected override void OnDetaching()
{
if (_textBox == null)
{
return;
}
_textBox.GotFocus -= TextBoxGotFocus;
_textBox.TextChanged -= TextBoxGotFocus;


base.OnDetaching();
}


private void TextBoxGotFocus(object sender, RoutedEventArgs routedEventArgs)
{
_textBox.CaretIndex = _textBox.Text.Length;
}
}

For some reasons I had to use :

Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Background, new Action(() =>
{
textBox.CaretIndex = textBox.Text.Length;
textBox.ScrollToEnd();
}));

I wanted to create a UserControl / View with a pre-populated textbox bound to a ViewModel, and when the control opens up, focus is automatically set on the textbox and the caret position at the end. This was the only way I got it to work:

public TextBoxDialogView()
{
InitializeComponent();


TextBox.GotKeyboardFocus += (sender, args) =>
{
TextBox.CaretIndex = TextBox.Text.Length;
};
_ = TextBox.Focus();
}

Seems to work nicely so far...