Click here to Skip to main content
15,893,564 members
Articles / Desktop Programming / XAML
Tip/Trick

Text Binding to WinRT RichTextBlock

Rate me:
Please Sign up or sign in to vote.
4.75/5 (4 votes)
1 May 2013CPOL 21.8K   295   2   3
The tip gives an easy way to bind string values to WinRT RichTextBlock

Introduction

Normally, it is not possible to bind text to WinRT RichTextBlock as we do for TextBlock. RichTextBlock is devoid of Text dependency property. So the only way is to populate the Blocks property with paragraphs in code behind. But by simply declaring an attached property, we can achieve binding to text in RichTextBlock.

Using the Code

The callback of attached property will create a new paragraph and add it to blocks of RichTextBlock. The Inlines property of RichTextBlock contains Run objects, whose Text is set to new value of the property.

C#
public static string GetText(DependencyObject obj)
{
     return (string)obj.GetValue(TextProperty);
}

public static void SetText(DependencyObject obj, string value)
{
     obj.SetValue(TextProperty, value);
}

// Using a DependencyProperty as the backing store for Text.  
// This enables animation, styling, binding, etc.
public static readonly DependencyProperty TextProperty =
       DependencyProperty.RegisterAttached("Text", typeof(string), 
       typeof(BindingHelper), new PropertyMetadata(String.Empty, OnTextChanged));

private static void OnTextChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
     var control = sender as RichTextBlock;
     if (control != null)
     {
          control.Blocks.Clear();
          string value = e.NewValue.ToString();

          var paragraph = new Paragraph();
          paragraph.Inlines.Add(new Run {Text = value});
          control.Blocks.Add(paragraph);
     }
} 

The XAML side binding will look like below:

XML
<RichTextBlock common1:BindingHelper.Text="{Binding ElementName=calendar, Path=SelectedDate}"/>

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
India India
Jawahar working as a Senior Development Engineer in Aditi Technologies,Bangalore, India. Specialist in all XAML frameworks. Very passionate on UX Design and Development. Skilled in Expression Blend, Design, WPF, Silverlight, Windows Phone 7/8, Windows 8. Good knowledge in Entity Framework, SQLite and SQL Server also. Also had good experience with PRISM, MVVM, Caliiburn Micro and other design patterns.

He developed few products for Syncfusion Inc. Also working on some freelancing projects. Worked as a lead developer of Metro Studio from Syncfusion Inc.

An active freelancer. http://xamlfactory.elance.com

http://about.me/jawahars

http://wpfplayground.com/

Comments and Discussions

 
QuestionHow to highlight text in richtextblock after binding using above approach Pin
Member 129395469-Feb-20 20:59
Member 129395469-Feb-20 20:59 
QuestionVery nice! Pin
Eric Hilliarde7-Aug-14 18:51
Eric Hilliarde7-Aug-14 18:51 
GeneralMy vote of 4 Pin
Daveedu22-Aug-13 0:29
Daveedu22-Aug-13 0:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.