繁体   English   中英

如何将数据从富文本框传输到另一个富文本框 WPF C#

[英]how to transfer data from richtextbox to another richtextbox WPF C#

嗨,我在将富文本框中的数据显示或传输到其他富文本框时遇到问题...

richtextbox1.Document = richtextbox2.Document; //This will be the idea..

实际上我打算做的是,我想将我的数据从我的数据库传输到我的列表视图,它将显示为

SQLDataEHRemarks = myData["remarks"].ToString();// Here is my field from my database which is set as Memo
RichTextBox NewRichtextBox = new RichTextBox();// Now i created a new Richtextbox for me to take the data from SQLDataEHRemarks...
NewRichtextBox.Document.Blocks.Clear();// Clearing
TextRange tr2 = new TextRange(NewRichtextBox.Document.ContentStart, NewRichtextBox.Document.ContentEnd);// I found this code from other forum and helps me a lot by loading data from the database....
MemoryStream ms2 = GetMemoryStreamFromString(SQLDataEHRemarks);//This will Convert to String
tr2.Load(ms2, DataFormats.Rtf);//then Load the Data to my NewRichtextbox

现在我想要做的是,我要将这些数据加载到我的 ListView .. 或其他控件,如文本块或文本框...

_EmpHistoryDataCollection.Add(new EmployeeHistoryObject{
EHTrackNum = tr2.ToString()  // The problem here is it will display only the first line of the paragraph.. not the whole paragraph
}); 

使用TextRangeText属性而不是.ToString()

以字符串形式获取RichTextBox内容的方法:

public static string GetStringFromRichTextBox(RichTextBox richTextBox)
{
    TextRange textRange = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd);
    return textRange.Text;
}

获取RichTextBox内容为富文本的方法:

public static string GetRtfStringFromRichTextBox(RichTextBox richTextBox)
{
    TextRange textRange = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd);
    MemoryStream ms = new MemoryStream();
    textRange.Save(ms, DataFormats.Rtf);

    return Encoding.Default.GetString(ms.ToArray());
}

编辑:您可以通过执行以下操作将 GetRtfStringFromRichTextBox() 返回的富文本放入另一个RichText控件中:

FlowDocument fd = new FlowDocument();
MemoryStream ms = new MemoryStream(Encoding.ASCII.GetBytes(richTextString));
TextRange textRange = new TextRange(fd.ContentStart, fd.ContentEnd);
textRange.Load(ms, DataFormats.Rtf);
richTextBox.Document = fd;

不会将 RichTextBox 的内容作为字符串获取,只是类似于以下内容(在 VB.Net 中)

Dim strText as string = MyRTB.text

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM