Bindable Run

Since the initial release of WPF, Run.Text has been a normal CLR property. This has meant that Run.Text lacks all the benefits of the WPF dependency property system, most notably the ability to be bound. In some cases, one could substitute Runs for TextBlocks, which can be bound to; however this can quickly create text flow problems (see example below). The omission of a bindable Run has given birth to many homebrew solutions (eg. here and here). Feeble workarounds and custom solutions are no longer necessary. In WPF 4.0 we have done the work to back Run.Text with a dependency property.



Bindable Run API

The property Run.Text has been converted to a dependency property . Now Run.Text enjoys all of the benefits of full dependency properties (binding, styling, templating, etc…).


<Window x:Class="TextBlogDWriteDemo.Window1"





        <TextBox x:Key="DataStore1" Text="how text does not

                                          flow correctly"/>

        <TextBox x:Key="DataStore2" Text="This is an example

  of how text flows correctly when only

  using bound Runs"/>




                <Run Text="This is an example of"/>

                <TextBlock Text="{Binding Source=

                   {StaticResource DataStore1},


                <Run Text="when using a combination of Runs

                           and TextBlock"/>



                <Run Text="{Binding Source={StaticResource

                           DataStore2}, Path=Text}"/>





This is a screenshot of the text produced by the above XAML. Notice the spacing issues which are fixed by binding to a Run as opposed to a TextBlock.

Bindable Run Usage


One way data binding is fully supported. A Run can be bound to a data source and the content of the Run will reflect the value of what it is bound to. The bound Run will receive and display any changes that occur in the data source.

Two way data binding is partially supported. If a bound Run is updated via calls to the WPF property system, the data source which the Run is bound to will reflect the changes to the Run. On the other hand, if a bound Run is updated via a RichTextBox or the text object model, the Run will lose its binding.


- Chipalo