如何在不滚动的情况下查找文本框中的文本数量

时间:2013-02-03 19:17:11

标签: c# textbox windows-runtime winrt-xaml

我的文字比较大。我需要将一定数量的此文本添加到文本框中,以便在不滚动的情况下显示该文本,然后将其余文本添加到另一个文本框,然后再添加另一个文本框。-.-。>循环遍历文本,根据需要生成尽可能多的文本框。

我的问题是我不知道如何找出每个文本框中有多少文本。到目前为止,我唯一能做的就是分配一个适合页面的固定数量的字符。但这不适用于不同的屏幕分辨率。有没有办法,技巧或解决方法,我可以用来计算文本中有多少文本可以适应固定字体和fontsize但相对宽度和高度的文本框?

int TextLength = 1000, PageStart = 0;

List<TextBox> Pages = new List<TextBox>();  
while (PageStart < TextLength) 
{
     TextBox p = new TextBox();
      if (PageStart + PageLength < TextLength)
      {
             p.PageText = Text.Substring(PageStart, PageLength);
             PageStart += PageLength;
             Pages.Add(p);
      }
      else
      {
             PageLength = TextLength - PageStart;
             p.PageText = Text.Substring(PageStart, PageLength);
             Pages.Add(p);
             break;
       }                      
  }

1 个答案:

答案 0 :(得分:1)

您可能更适合使用TextBlock。除此之外,TextBlock测量技术也适用于TextBoxes - how to calculate the textbock height and width in on load if i create textblock from code?

您需要在增加文本量的同时测量ActualHeight,直到超出限制为止。