Пробуем следующий вариант:
| Код | public class MyRichTextBox : RichTextBox { private static DependencyProperty SaveAsProperty = DependencyProperty.Register("SaveAs", typeof(string), typeof(MyRichTextBox)); private static DependencyProperty LoadedAsProperty = DependencyProperty.Register("LoadedAs", typeof(string), typeof(MyRichTextBox)); private static DependencyProperty IsFormattedProperty = DependencyProperty.Register("IsFormatted", typeof(bool), typeof(MyRichTextBox), new PropertyMetadata(false)); private static DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(MyRichTextBox)); public string Text { get { return (string)GetValue(TextProperty); } set { SetValue(TextProperty, value); } } public MyRichTextBox() : base() { } public MyRichTextBox(FlowDocument document) : base(document) { IsFormatted = true; } protected override void OnInitialized(EventArgs e) { base.OnInitialized(e); var descriptor = DependencyPropertyDescriptor.FromProperty(TextProperty, typeof(TextBox)); descriptor.AddValueChanged(this, delegate { if (RTBContent != Text) RTBContent = Text == null ? string.Empty : Text; }); TextChanged += delegate { if (Text != RTBContent) Text = RTBContent; }; } public bool IsFormatted { get { return (bool)GetValue(IsFormattedProperty); } set { SetValue(IsFormattedProperty, value); } }
public string SaveAs { get { return (string)GetValue(SaveAsProperty); } set { SetValue(SaveAsProperty, value); } }
public string LoadedAs { get { return (string)GetValue(LoadedAsProperty); } set { SetValue(LoadedAsProperty, value); } }
public string TextOnly { get { return new TextRange(Document.ContentStart, Document.ContentEnd).Text; } }
public void ClearFormatting() { RTBContent = TextOnly; } public string RTBContent { get { TextRange tr = new TextRange(Document.ContentStart, Document.ContentEnd); using (var ms = new MemoryStream()) { tr.Save(ms, IsFormatted ? DataFormats.Rtf : DataFormats.Text); return ASCIIEncoding.Default.GetString(ms.ToArray()); } } set { TextRange selection = new TextRange(Document.ContentStart, Document.ContentEnd); SetRangeContent(selection, value);
if ((string)GetValue(TextBox.TextProperty) != value) SetValue(TextBox.TextProperty, value); } } private string SetRangeContent(TextRange selection, string content) { string loadType = DataFormats.Rtf; IsFormatted = true; content = content == "" ? " " : content; try { using (MemoryStream ms = new MemoryStream(ASCIIEncoding.Default.GetBytes(content))) { if (content.StartsWith("<Section")) { if (selection.CanLoad(DataFormats.Xaml)) { selection.Load(ms, DataFormats.Xaml); loadType = DataFormats.Xaml; } } else if (content.StartsWith("PK")) { if (selection.CanLoad(DataFormats.XamlPackage)) { selection.Load(ms, DataFormats.XamlPackage); loadType = DataFormats.XamlPackage; } } else if (content.StartsWith("{\\rtf")) { if (selection.CanLoad(DataFormats.Rtf)) { selection.Load(ms, DataFormats.Rtf); loadType = DataFormats.Rtf; } } else { if (selection.CanLoad(DataFormats.Text)) { selection.Load(ms, DataFormats.Text); loadType = DataFormats.Text; IsFormatted = false; } } } } catch (Exception) { throw; } return loadType; } }
|
Используем:
| Код | <local:MyRichTextBox Text="{Binding Path=MyField}" />
|
|