using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace Kaitone.DetectiveHelper.UI.Controls.RichTextBox { /// <summary> /// RichboxTextShow.xaml 的互動邏輯 /// </summary> public partial class RichboxTextShow : UserControl { public static readonly DependencyProperty TextyProperty = DependencyProperty.Register("Text",typeof(string), typeof(RichboxTextShow),new PropertyMetadata(string.Empty,new PropertyChangedCallback(TextyPropertyChanged) )); private static void TextyPropertyChanged(DependencyObject sender,DependencyPropertyChangedEventArgs args) { RichboxTextShow editer = new RichboxTextShow(); } public string Text { get { return ConvertToRtfString(this.mainRTB); } set { if (!String.IsNullOrEmpty(value)) { LoadFromRtfString(value,this.mainRTB); } else { this.mainRTB.Document.Blocks.Clear(); } } } private static void LoadFromRtfString(string rtf, System.Windows.Controls.RichTextBox richTextBox) { if (string.IsNullOrEmpty(rtf)) { throw new ArgumentNullException(); } TextRange textRange = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd); using (MemoryStream ms = new MemoryStream()) { using (StreamWriter sw = new StreamWriter(ms)) { sw.Write(rtf); sw.Flush(); ms.Seek(0, SeekOrigin.Begin); textRange.Load(ms, DataFormats.Rtf); } } } private static string ConvertToRtfString(System.Windows.Controls.RichTextBox rcb) { string resultstring = string.Empty; using (MemoryStream stream=new MemoryStream()) { TextRange documentTextRange = new TextRange(rcb.Document.ContentStart, rcb.Document.ContentEnd); string dataformt = DataFormats.Rtf; documentTextRange.Save(stream, dataformt); stream.Seek(0, SeekOrigin.Begin); using(StreamReader reader=new StreamReader(stream,Encoding.Default)){ resultstring = reader.ReadToEnd(); } } return resultstring; } public RichboxTextShow() { InitializeComponent(); } } } |