Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将TextChanged事件中的RichTextBox文本设置为另一个RichTextBox的更快方法?_C#_Wpf_Richtextbox_Rtf - Fatal编程技术网

C# 将TextChanged事件中的RichTextBox文本设置为另一个RichTextBox的更快方法?

C# 将TextChanged事件中的RichTextBox文本设置为另一个RichTextBox的更快方法?,c#,wpf,richtextbox,rtf,C#,Wpf,Richtextbox,Rtf,我在一个允许实时编辑的程序中工作,在只读模式下使用RTbox显示文本 为了便于编写/阅读文本,我创建了以下自定义元素类: public class Texts : RichTextBox { Paragraph p = new Paragraph(); FlowDocument fd = new FlowDocument(); TextRange tr; public Texts() {

我在一个允许实时编辑的程序中工作,在只读模式下使用RTbox显示文本

为了便于编写/阅读文本,我创建了以下自定义元素类:

    public class Texts : RichTextBox
    {
        Paragraph p = new Paragraph();
        FlowDocument fd = new FlowDocument();
        TextRange tr;

        public Texts()
        {
            //_RtStylish is a class I made to create all the Brushes, Bitmaps and fonts stuff. It's kind of long to do this for each one so I implemented it to just write a simple line with my class.
            Background = _RtStylish._tr_cl;
            Foreground = _RtStylish._stdCl;

            BorderThickness = _RtStylish.t_flat;
            Width = 100;
            Height = 30;
            FontFamily = _RtStylish._font("Arial");
            FontSize = 40;
            Document = fd;
        }

        public void _text(string txt)
        {
            p.Inlines.Add(txt);
            fd.Blocks.Add(p);
//To add text I just write RichTextBox._text("Something");
        }



        public string _readText()
        {
            tr = new TextRange(fd.ContentStart, fd.ContentEnd);
            return tr.Text;
        }
//This returns a string with the text inside the RTbox

        public void _Cleartxt()
        {
            p.Inlines.Clear();
            fd.Blocks.Clear();
            p.Inlines.Add("");
            fd.Blocks.Add(p);
        }
//Clears my box

        public void _alignment(int n)
        {
            switch (n) 
            { 
                case 0:
            fd.TextAlignment = TextAlignment.Center;
            break;
                case 1:
            fd.TextAlignment = TextAlignment.Justify;
            break;
                case 2:
            fd.TextAlignment = TextAlignment.Left;
            break;
                case 3:
            fd.TextAlignment = TextAlignment.Right;
            break;
            }
        }


    }
问题是,我有第二个窗口,它是真实窗口的缩放副本,允许更改RTbox中的文本以进行实时编辑。因此,我所做的就是举办这个活动,并把它送给所有的富盒子:

  private void _text(Object o, TextChangedEventArgs e)
    {
        if(_Tmod.IsFocused)
        {
            _title._Cleartxt();
            _title._text(_Tmod._readText());
        }
        else if(_Qmod.IsFocused)
        {
            _question._Cleartxt();
            _question._text(_Qmod._readText());
        }
        else if(_Amod.IsFocused)
        {
            _ans._Cleartxt();
            _ans._text(_Amod._readText());
        }

    }
如果我不清除文本框,它将保留旧文本+=新文本。例如,如果我在主窗口中有这样的文本:“Hellowww”,然后我通过RTbox编辑器修改它,并添加一个“a”,它会显示:“HellowHellowa”,因此我必须清除该框

它工作得很好,但是当用户在编辑器的文本中键入文本时,它在主窗口中显示时有点滞后。我想这是因为每次我更改文本时,我都会清除Box文档并添加一个新块。当一个文本框的文本发生变化时,有没有一种更快的方法将其分配给另一个文本框

在WinForms中,它就像
TextBox1.Text=EditorTextbox.Text并且没有延迟。我所说的lagg是指在写一篇长文章之间需要一些停顿。尽管如此,它并没有遗漏任何角色,但我不喜欢该程序看起来很滞后:(

在WinForms中有没有类似的改进方法?

如果您使用,您可以使用它来做您想做的事情。如果您希望相同的文本出现在两个
TextBox
es中,那么您可以将相同的
FlowDocument
属性绑定到两个
RichTextBox
es上的
Document
属性:

<Prefix:BindableRichTextBox Name="TextBox1" Document="{Binding YourProperty}" />
...
<Prefix:BindableRichTextBox Name="TextBox2" Document="{Binding YourProperty}" />


...

您是否考虑过数据绑定?
public FlowDocument YourProperty
{
    get { return yourProperty; }
    set
    {
        yourProperty = value;
        NotifyPropertyChanged("YourProperty");
        NotifyPropertyChanged("YourEditedProperty");
    }
}

public FlowDocument YourEditedProperty
{
    get { return SomeManipulationMethod(YourProperty); }
}
<Prefix:BindableRichTextBox Name="TextBox1" Document="{Binding YourProperty}" />
...
<Prefix:BindableRichTextBox Name="TextBox2" Document="{Binding YourEditedProperty}" />