如何在没有 NoWrap 的 TextBox 中跟随文本的结尾?

How to follow the end of a text in a TextBox with no NoWrap?(如何在没有 NoWrap 的 TextBox 中跟随文本的结尾?)
本文介绍了如何在没有 NoWrap 的 TextBox 中跟随文本的结尾?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 xaml 中有一个文本框:

<文本框名称=文本";Horizo​​ntalAlignment =左"高度=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 中跟随文本的结尾?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)