Hi
Using below user control you can bind datasource with richtext box
RichTextControl.xaml
<usercontrol x:class="Controls.RichTextControl" xmlns:x="#unknown">
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<grid>
<richtextbox x:name="RTextBox" verticalscrollbarvisibility="Auto" />
</grid>
</usercontrol>
RichTextControl.xaml.cs
public partial class RichTextControl : UserControl
{
public RichTextControl()
{
InitializeComponent();
}
public static readonly DependencyProperty DocumentProperty =
DependencyProperty.Register("Document", typeof(FlowDocument),
typeof(RichTextControl), new PropertyMetadata(OnDocumentChanged));
public FlowDocument Document
{
get { return (FlowDocument)GetValue(DocumentProperty); }
set { SetValue(DocumentProperty, value); }
}
private bool _IsReadOnly = false;
public bool IsReadOnly
{
get
{
return RTextBox.IsReadOnly;
}
set
{
_IsReadOnly = value;
RTextBox.IsReadOnly = _IsReadOnly;
}
}
private static void OnDocumentChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var thisControl = (RichTextControl)d;
thisControl.RTextBox.Document = (e.NewValue == null) ? new FlowDocument() : (FlowDocument)e.NewValue;
}
public void UpdateDocumentBindings()
{
SetValue(DocumentProperty, this.RTextBox.Document);
}
}
RichTextConverter.cs
public class RichTextConverter : IValueConverter
{
public object Convert(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
var flowDocument = new FlowDocument();
if (value != null)
{
var xamlText = (string)value;
flowDocument = (FlowDocument)XamlReader.Parse(xamlText);
}
return flowDocument;
}
public object ConvertBack(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value == null) return string.Empty;
var flowDocument = (FlowDocument)value;
return XamlWriter.Save(flowDocument);
}
}
Use below to set binding
<control:richtextcontrol x:name="RTMail" document="{Binding Path=Message, Converter={StaticResource RichTextConverter}, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged,ValidatesOnDataErrors=True,NotifyOnValidationError=True}" xmlns:x="#unknown" xmlns:control="#unknown"></control:richtextcontrol>