Reputation: 118
SOLUTION: As mm8 in the comments pointed, the solution is with ScrollToHorizontalOffset. I got it to work by modifying mm8's answer and here's the working code:
inputTextBox.ScrollToHorizontalOffset(inputTextBox.GetRectFromCharacterIndex(inputTextBox.Text.Length).Right);
It's a little lengthy, but it works perfectly!
I have a program with two text boxes:
<TextBox x:Name="InputTextBlock" Grid.Row="1" Grid.Column="1" Margin="5,0,5,0"
Text="{Binding FileToConvert, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
<TextBox x:Name="OutputTextBlock" Grid.Row="3" Grid.Column="1" Margin="5,0,5,0"
Text="{Binding OutputFilePath, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
When one of the text boxes gets updated, the second one is updated as well with stuff, but both of the text boxes should scroll to the end of the text inserted. This is because sometimes those input/output directories can be very long.
I already tried to get those two textboxes, and edit their properties in the code behind like this:
...
var inputTextBox = (System.Windows.Controls.TextBox) grid.GetElementByName("InputTextBlock");
var outputTextBox = (System.Windows.Controls.TextBox) grid.GetElementByName("OutputTextBlock");
inputTextBox.Focus();
inputTextBox.Select(inputTextBox.Text.Length, 0);
inputTextBox.ScrollToEnd();
outputTextBox.Focus();
outputTextBox.Select(outputTextBox.Text.Length, 0);
outputTextBox.ScrollToEnd();
But after this, only the later is scrolled to the end. Is there a way to scroll these two textboxes to the end?
The binded text fields (FileToConvert
and OutputFilePath
) are being updated with buttons and stuff.
Upvotes: 1
Views: 70
Reputation: 169420
You could handle the TextChanged
event like this:
void OnTextChanged(object sender, TextChangedEventArgs e)
{
TextBox textBox = (TextBox)sender;
textBox.ScrollToHorizontalOffset(textBox.GetRectFromCharacterIndex(textBox.CaretIndex)
.Right);
}
InputTextBlock.TextChanged += OnTextChanged;
OutputTextBlock.TextChanged += OnTextChanged;
Upvotes: 1