从WPF RichTextBox

时间:2016-02-17 09:13:34

标签: wpf richtextbox

如何从代码中编辑RichTextBox中的特定行?

我在RichTextBox

中添加了一行
FlowDocument mcFlowDoc = new FlowDocument();

Paragraph para = new Paragraph();
para.Inlines.Add(new Run("I am a RichTextBox control line 1\n"));
para.Inlines.Add(new Run("I am a RichTextBox control line 2\n"));
para.Inlines.Add(new Run("I am a RichTextBox control line 3\n")
{
    Foreground = Brushes.Red
});

mcFlowDoc.Blocks.Add(para);

RichTextBox1.Document = mcFlowDoc;

我的XAML

<RichTextBox Margin="5" Name="RichTextBox1" FontSize="16" VerticalAlignment="Top" 
             Width="500" Height="220">
</RichTextBox>

现在我想在点击它时添加一个按钮(点击事件),第二行将变为&#34;我终于要做到了#34;

3 个答案:

答案 0 :(得分:1)

替换这样的一行:

private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
    {
        string lineToReplace = "I am a RichTextBox control line 2";
        string newLine = "I finally got to do it";
        TextRange text = new TextRange(RichTextBox1.Document.ContentStart, RichTextBox1.Document.ContentEnd);
        TextPointer current = text.Start.GetInsertionPosition(LogicalDirection.Forward);
        while (current != null)
        {
            string textInRun = current.GetTextInRun(LogicalDirection.Forward);
            if (!string.IsNullOrWhiteSpace(textInRun))
            {
                int index = textInRun.IndexOf(lineToReplace);
                if (index != -1)
                {
                    TextPointer selectionStart = current.GetPositionAtOffset(index, LogicalDirection.Forward);
                    TextPointer selectionEnd = selectionStart.GetPositionAtOffset(lineToReplace.Length, LogicalDirection.Forward);
                    TextRange selection = new TextRange(selectionStart, selectionEnd);
                    selection.Text = newLine;
                    selection.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Bold);
                    RichTextBox1.Selection.Select(selection.Start, selection.End);
                    RichTextBox1.Focus();
                }
            }
            current = current.GetNextContextPosition(LogicalDirection.Forward);
        }
    }

答案 1 :(得分:0)

如果您的文本结构始终相同,您只需找到所需的运行并在单击处理程序中更改它,如下所示:

var par = (Paragraph) RichTextBox1.Document.Blocks.FirstOrDefault();
var run = (Run) par.Inlines.Skip(1).First();
run.Text = "I finally got to do it\n";

答案 2 :(得分:0)

又快又脏:

private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
    {
        TextRange textRange = new TextRange(
            // TextPointer to the start of content in the RichTextBox.
         RichTextBox1.Document.ContentStart,
            // TextPointer to the end of content in the RichTextBox.
         RichTextBox1.Document.ContentEnd
       );

        // get the lines of text
        string[] lines =textRange.Text.Split(new[] { Environment.NewLine } , StringSplitOptions.RemoveEmptyEntries);

        // get the second line
        lines[1] = "I finally got to do it" + Environment.NewLine;

        // build a string from the string array
        StringBuilder builder = new StringBuilder();
        foreach (string value in lines)
        {  
            builder.Append(value);
        }

        // test the text in the RichTextbox
        Paragraph para = new Paragraph();
        para.Inlines.Add(new Run(builder.ToString()));
        RichTextBox1.Document.Blocks.Clear();
        RichTextBox1.Document.Blocks.Add(para);
    }

并在文档中正确添加换行符:

FlowDocument mcFlowDoc = new FlowDocument();

        Paragraph para = new Paragraph();
        para.Inlines.Add(new Run("I am a RichTextBox control line 1"));
        para.Inlines.Add(Environment.NewLine);
        para.Inlines.Add(new Run("I am a RichTextBox control line 2"));
        para.Inlines.Add(Environment.NewLine);
        para.Inlines.Add(new Run("I am a RichTextBox control line 3")
        {
            Foreground = Brushes.Red
        });

        mcFlowDoc.Blocks.Add(para);

        RichTextBox1.Document = mcFlowDoc;