|
RadControls version
|
2010 Q2 |
| .NET version |
Silverlight 4 |
| Visual Studio version |
2010 |
| programming language |
C# |
SAMPLE DESCRIPTION
Problem
Bind the content of RadRichTextBox in Xaml is not as trivial as MS RichTextBox (which simply exposes a Xaml property) as RadRichTextBox provides various input and output formats.
Update from Telerik: Please note that since 2010 Q2 Telerik introduced specially designed data-providers, that can be used to bind RadRichTextBox content as Xaml, Html, Text and so on. The approach is described in the following help article:
http://www.telerik.com/community/code-library/silverlight/general/binding-the-content-of-radrichtextbox-in-xaml.aspx
A sample for using data-providers is attached. The approach suggested by the article bellow, still applies and can be useful especially in scenarios when additional custom processing of the content is required.
Solution.
Create a custom, bindable RadRichEditor-Control (currently supports only One-Way binding), with a DependencyProperty that exposes RadRichTextBox content.
It should be easy to extend to support Two-Way binding...
Usefulfor :
-> Showing ToolTips
-> Previews
-> ReadOnly text
Codebehind of the UserControl :
public partial class RichEdit : UserControl
{
public string Text
{
get { return (string)GetValue(TextProperty); }
set { SetValue(TextProperty, value); }
}
public static readonly DependencyProperty TextProperty =
DependencyProperty.Register("Text", typeof(string), typeof(RichEdit), new PropertyMetadata(null, OnTextPropertyChanged));
private static void OnTextPropertyChanged(DependencyObject re, DependencyPropertyChangedEventArgs e)
{
RichEdit richEdit = (RichEdit)re;
if (string.IsNullOrEmpty((string)e.NewValue) == false)
{
richEdit.richTextBox.Document = ImportXaml(e.NewValue.ToString());
}
}
public RichEdit()
{
InitializeComponent();
}
private static RadDocument ImportXaml(string content)
{
IDocumentFormatProvider provider = new XamlFormatProvider();
RadDocument document;
using (MemoryStream stream = new MemoryStream())
{
StreamWriter writer = new StreamWriter(stream);
writer.Write(content);
writer.Flush();
stream.Seek(0, SeekOrigin.Begin);
document = provider.Import(stream);
}
return document;
}
}
XAML of the UserControl:
I hope you'll find ti useful.
Best Greetings
Cyril Iselin