我在一個允許實時編輯的程序中工作,在只讀模式下使用RTboxes顯示文本。將TextChanged事件中的RichTextBox文本設置爲另一個Richtextbox的更快捷方式?
我爲了使其易於讀/寫我做了這個自定義元素類的文字:
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;
那樣沒有lagg。 lagg我的意思是在編寫長文本之間需要一些暫停。即便如此,它不會錯過任何字符,但我不喜歡這個項目,看看laggy :(
任何改進的方式來做到這一點像的WinForms?
你有沒有考慮數據綁定? – user2588666