【问题标题】:Controlling the Length of a RichTextBox in wpf C#在 wpf C# 中控制 RichTextBox 的长度
【发布时间】:2015-07-29 12:22:00
【问题描述】:

我需要做这样的事情Controlling the Length of a RichTextBox in C#

但在 WPF 中:

if (richTextBox.Text.Length > maxsize)
            {
                // this method preserves the text colouring
                // find the first end-of-line past the endmarker

                Int32 endmarker = richTextBox.Text.IndexOf('\n', dropsize) + 1;
                if (endmarker < dropsize)
                    endmarker = dropsize;

                richTextBox.Select(0, endmarker);
                richTextBox.Cut();
            }

这会引发编译错误

错误 1 ​​'System.Windows.Controls.RichTextBox' 不包含 'Text' 的定义并且没有扩展方法 'Text' 接受第一个 可以找到“System.Windows.Controls.RichTextBox”类型的参数 (您是否缺少 using 指令或程序集引用?)

错误 2 方法 'Select' 没有重载需要 2 个参数

显然 Text 属性在 WPF 中不起作用,请参阅此页面 RichTextBox (WPF) does not have string property “Text” 并提出此问题,我找到了属性 Text 的解决方案,但没有找到 Select 方法的解决方案。

 var myText = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd);

            if (myText.Text.Length > maxsize)
            {
                // this method preserves the text colouring
                // find the first end-of-line past the endmarker

                Int32 endmarker = myText.Text.IndexOf('\n', dropsize) + 1;
                if (endmarker < dropsize)
                    endmarker = dropsize;

                richTextBox.Select(0, endmarker);//No overload for method 'Select' takes 2 arguments
                myText.Select(0, endmarker);//No overload for method 'Select' takes 2 arguments
                console.Cut();
            }

那么现在,我如何在 WPF 中实现这一点。

提前致谢。

【问题讨论】:

  • 我以前没用过WPF,但它是Document 属性吗? MSDN 将其描述为“获取或设置表示 RichTextBox 内容的 FlowDocument。”
  • @Ric 我更新了我的问题,发现解决了属性 Text 的问题,但不适用于 Select 方法。

标签: c# wpf richtextbox


【解决方案1】:

RichTextBox 没有 Text 属性,你现在正在做的是要走的路,你可以通过将它移动到全局扩展方法来改进你的代码:

  public static class Extentions
    {
        public static string Text(this RichTextBox richTextBox)
        {
            TextRange content = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd);
            return content.Text;           
        }
    }

编辑:
如果您的最终目标是从控件中选择一些文本,则根本不需要 Text 属性:

对于 RichTextBox 选择,您必须使用 TextPointer:

TextPointer text = richTextBox.Document.ContentStart;
while (text.GetPointerContext(LogicalDirection.Forward) != TextPointerContext.Text)
{
    text = text.GetNextContextPosition(LogicalDirection.Forward);
}
TextPointer startPos = text.GetPositionAtOffset(0);
TextPointer endPos = text.GetPositionAtOffset(endmarker);
var textRange = new TextRange(startPos, endPos);
textRange.ApplyPropertyValue(TextElement.BackgroundProperty, new SolidColorBrush(Colors.Blue));


编辑 2:将文本附加到 RichTextEditors。

private void processLine(string line)
{
    //You can clear if necessary
    //richTextBox.Document.Blocks.Clear();

    //whatever your logic. I'm only taking the first 10 chars
    string trimmed = text.Substring(0,9); 
    richTextBox.Document.Blocks.Add(new Paragraph(new Run(trimmed)));
}

【讨论】:

  • 我更新了我的问题,这是属性 Text 的解决方案,但不是 Select 方法。
  • 真的我需要在文本达到一定大小时修剪文本。所以 GetPositionAtOffset 和 TextPointer 是解决选择一些文本问题的好主意。我不明白while循环的代码。为了简化和澄清,我需要一种接收文本行并将此行附加到 RichTextbox 中的文本的方法,以防数据量变得太大,在达到一定大小时修剪文本。
  • 所以你正在从另一个来源接收文本,并且在验证后,你想将它附加到 RichTextBox?
  • 是的,这就是我想要的
  • 好的,你现在可以查看了
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-02-29
  • 1970-01-01
  • 1970-01-01
  • 2018-03-06
  • 1970-01-01
  • 1970-01-01
  • 2019-07-19
相关资源
最近更新 更多