【问题标题】:Faster way to set a RichTextBox text in TextChanged event to another Richtextbox?将 TextChanged 事件中的 RichTextBox 文本设置为另一个 Richtextbox 的更快方法?
【发布时间】:2014-07-14 05:46:03
【问题描述】:

我正在使用一个允许实时编辑的程序,使用只读模式下的 RTboxes 来显示文本。

我为了方便书写/阅读文本我制作了这个自定义元素类:

    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;
            }
        }


    }

问题是我有第二个窗口,它是真实窗口的缩放副本,允许更改 RTboxes 中的文本以进行实时编辑。所以我做的就是这个事件,并把它交给所有的富人盒子:

  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; 并且没有滞后。滞后是指在写长文本之间需要一些停顿。即使它没有遗漏任何字符,但我不喜欢程序看起来滞后:(

在 WinForms 中有什么改进的方法吗?

【问题讨论】:

  • 你考虑过数据绑定吗?

标签: c# wpf richtextbox rtf


【解决方案1】:

如果你使用A Bindable WPF RichTextBox,你可以使用data binding 做你想做的事。如果您希望相同的文本出现在两个TextBoxes 中,那么您只需将相同的FlowDocument 属性绑定到两个RichTextBoxes 上的Documentproperty:

<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}" />

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-11-24
    • 1970-01-01
    • 2014-03-14
    • 1970-01-01
    • 2014-09-17
    • 2011-07-16
    • 1970-01-01
    相关资源
    最近更新 更多