我在 xaml
中有一个文本框:
<文本框名称=文本";HorizontalAlignment =左"高度=75"VerticalContentAlignment =中心"TextWrapping =NoWrap"文本=文本框"宽度=336"BorderBrush =黑色"字体大小=40"/>
我用这个方法给它添加文字:
private string words = "TextBox 的初始文本内容.";公共异步无效文本旋转(){for(int a =0; a < words.Length; a++){Text.Text = words.Substring(0,a);等待任务.延迟(500);}}
一旦文本脱离包装,有一种方法可以聚焦结尾,使旧文本消失在左侧,新文本消失在右侧,而不是仅仅将其添加到右侧而不看到.
一种快速的方法是测量需要滚动的字符串(words
)
I have a TextBox in xaml
:
<TextBox Name="Text" HorizontalAlignment="Left" Height="75" VerticalContentAlignment="Center" TextWrapping="NoWrap" Text="TextBox" Width="336" BorderBrush="Black" FontSize="40" />
I add text to it with this method:
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);
}
}
Once the text goes of out of the wrap is there a way to focus the end so the old text disappears to the left and the new on the right, as opposed to just adding it to the right without seeing.
A quick method is to measure the string (words
) that needs scrolling with TextRenderer.MeasureText, divide the width
measure in parts equals to the number of chars in the string and use ScrollToHorizontalOffset() to perform the scroll:
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);
}
}
Same, but using the FormattedText class to measure the string:
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);
}
}
这篇关于如何在没有 NoWrap 的 TextBox 中跟随文本的结尾?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!