如何在textchanged事件中更改richtextbox中的文本

时间:2019-02-27 03:09:36

标签: c# wpf

我想随用户输入更改RichTextBox的文本(粗体,下标等),所以这是我的代码:

private void FunctionRTB_TextChanged(object sender, TextChangedEventArgs e) {
        if (IsEditing) return;
        IsEditing = true;
        FlowDocument flowDocument = new FlowDocument();
        Paragraph paragraph = new Paragraph();
        //
        //populating 'paragraph'
        //
        flowDocument.Blocks.Add(paragraph);
        //using (FunctionRTB.DeclareChangeBlock()) {
            //FunctionRTB.BeginChange(); // tried this
            FunctionRTB.Document = flowDocument;
            //FunctionRTB.EndChange(); // with this, didnt work.
        //}
        IsEditing = false;
    }

可是:

System.InvalidOperationException: 'Cannot set the Document property inside the scope of DeclareChangeBlock or BeginChange/EndChange calls.'

在:

FunctionRTB.Document = flowDocument;

我尝试使用DeclareChangeBlock()BeginChange()/EndChange()。但是没有一个有效。

欢迎提出建议和链接。

0 个答案:

没有答案