How to Bind the text of RichTextArea from xaml
Asked
Active
Viewed 4.0k times
5 Answers
26
They've got the easier answer here:
Silverlight 4 RichTextBox Bind Data using DataContext and it works like a charm.
<RichTextBox>
<Paragraph>
<Run Text="{Binding Path=LineFormatted}" />
</Paragraph>
</RichTextBox>
-
2This also works great in Windows 8/8.1 XAML. Thanks!! – dex3703 Jan 30 '14 at 21:10
-
1Simple but effective solution, Brilliant – DNKROZ Feb 28 '14 at 15:26
-
5As @dex3703 was saying, this works in WPF with the syntax: `
-
Just an FYI for others.. this will fail if the user does a SelectAll and Delete since that paragraph element will be removed. – Bill Tarbell Oct 20 '17 at 00:30
4
You can use the InlineUIContainer
class if you want to bind a XAML control inside of an inline typed control
<RichTextBlock>
<Paragraph>
<InlineUIContainer>
<TextBlock Text="{Binding Name"} />
</InlineUIContainer>
</Paragraph>
</RichTextBlock>

jeuxjeux20
- 391
- 1
- 6
- 20

Kevin Tan
- 169
- 12
4
Here is the solution I came up with. I created a custom RichTextViewer class and inherited from RichTextBox.
using System.Windows.Documents;
using System.Windows.Markup;
using System.Windows.Media;
namespace System.Windows.Controls
{
public class RichTextViewer : RichTextBox
{
public const string RichTextPropertyName = "RichText";
public static readonly DependencyProperty RichTextProperty =
DependencyProperty.Register(RichTextPropertyName,
typeof (string),
typeof (RichTextBox),
new PropertyMetadata(
new PropertyChangedCallback
(RichTextPropertyChanged)));
public RichTextViewer()
{
IsReadOnly = true;
Background = new SolidColorBrush {Opacity = 0};
BorderThickness = new Thickness(0);
}
public string RichText
{
get { return (string) GetValue(RichTextProperty); }
set { SetValue(RichTextProperty, value); }
}
private static void RichTextPropertyChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
{
((RichTextBox) dependencyObject).Blocks.Add(
XamlReader.Load((string) dependencyPropertyChangedEventArgs.NewValue) as Paragraph);
}
}
}

e-rock
- 141
- 1
- 8
3
There is no built in way to do that. You can create Text attached property and bind to it like discussed here

Arsen Mkrtchyan
- 49,896
- 32
- 148
- 184
-
3The link provided is specific to WPF. The RichTextBox in Silverlight doesn't have a Document property on it. – Steve Wortham Sep 04 '10 at 21:37
-
0
This can't be done, you have to manually update it. Document is not a DependencyProperty.

Ana Betts
- 73,868
- 16
- 141
- 209