如何在没有 NoWrap 的情况下跟踪 TextBox 中文本的结尾?
How to follow the end of a text in a TextBox with no NoWrap?
我在 xaml
中有一个文本框:
<TextBox Name="Text" HorizontalAlignment="Left" Height="75" VerticalContentAlignment="Center" TextWrapping="NoWrap" Text="TextBox" Width="336" BorderBrush="Black" FontSize="40" />
我用这个方法添加文本:
private string words = "Initial text contents of the TextBox.";
public async void textRotation()
{
for(int a =0; a < words.Length; a++)
{
Text.Text = words.Substring(0,a);
await Task.Delay(500);
}
}
一旦文本脱离包装,是否有一种方法可以使末尾聚焦,这样旧文本就会消失在左侧,新文本会出现在右侧,而不是只是将其添加到右侧而看不到。
一个快速的方法是测量需要滚动的字符串(words
)用TextRenderer.MeasureText, divide the width
measure in parts equals to the number of chars in the string and use ScrollToHorizontalOffset()执行滚动:
public async void textRotation()
{
float textPart = TextRenderer.MeasureText(words, new Font(Text.FontFamily.Source, (float)Text.FontSize)).Width / words.Length;
for (int i = 0; i < words.Length; i++)
{
Text.Text = words.Substring(0, i);
await Task.Delay(100);
Text.ScrollToHorizontalOffset(textPart * i);
}
}
相同,但使用 FormattedText class 来测量字符串:
public async void textRotation()
{
var textFormat = new FormattedText(
words, CultureInfo.CurrentCulture, System.Windows.FlowDirection.LeftToRight,
new Typeface(this.Text.FontFamily, this.Text.FontStyle, this.Text.FontWeight, this.Text.FontStretch),
this.Text.FontSize, null, null, 1);
float textPart = (float)textFormat.Width / words.Length;
for (int i = 0; i < words.Length; i++)
{
Text.Text = words.Substring(0, i);
await Task.Delay(200);
Text.ScrollToHorizontalOffset(textPart * i);
}
}
应该很容易实现,尝试添加这段代码:
public async void textRotation()
{
for(int a =0; a < words.Length; a++)
{
Text.Text = words.Substring(0,a);
Text.ScrollToHorizontalOffset(Text.Text.Last());
await Task.Delay(500);
}
}
我在 xaml
中有一个文本框:
<TextBox Name="Text" HorizontalAlignment="Left" Height="75" VerticalContentAlignment="Center" TextWrapping="NoWrap" Text="TextBox" Width="336" BorderBrush="Black" FontSize="40" />
我用这个方法添加文本:
private string words = "Initial text contents of the TextBox.";
public async void textRotation()
{
for(int a =0; a < words.Length; a++)
{
Text.Text = words.Substring(0,a);
await Task.Delay(500);
}
}
一旦文本脱离包装,是否有一种方法可以使末尾聚焦,这样旧文本就会消失在左侧,新文本会出现在右侧,而不是只是将其添加到右侧而看不到。
一个快速的方法是测量需要滚动的字符串(words
)用TextRenderer.MeasureText, divide the width
measure in parts equals to the number of chars in the string and use ScrollToHorizontalOffset()执行滚动:
public async void textRotation()
{
float textPart = TextRenderer.MeasureText(words, new Font(Text.FontFamily.Source, (float)Text.FontSize)).Width / words.Length;
for (int i = 0; i < words.Length; i++)
{
Text.Text = words.Substring(0, i);
await Task.Delay(100);
Text.ScrollToHorizontalOffset(textPart * i);
}
}
相同,但使用 FormattedText class 来测量字符串:
public async void textRotation()
{
var textFormat = new FormattedText(
words, CultureInfo.CurrentCulture, System.Windows.FlowDirection.LeftToRight,
new Typeface(this.Text.FontFamily, this.Text.FontStyle, this.Text.FontWeight, this.Text.FontStretch),
this.Text.FontSize, null, null, 1);
float textPart = (float)textFormat.Width / words.Length;
for (int i = 0; i < words.Length; i++)
{
Text.Text = words.Substring(0, i);
await Task.Delay(200);
Text.ScrollToHorizontalOffset(textPart * i);
}
}
应该很容易实现,尝试添加这段代码:
public async void textRotation()
{
for(int a =0; a < words.Length; a++)
{
Text.Text = words.Substring(0,a);
Text.ScrollToHorizontalOffset(Text.Text.Last());
await Task.Delay(500);
}
}