I am working on a WPF application based on the MVVM pattern.
I have a RichTextBox, like so:
<RichTextBox Name="HtmlRichTextBox">
<FlowDocument>
<Paragraph>
Some Test
</Paragraph>
</FlowDocument>
</RichTextBox>
And a TextBox:
<TextBox Text="{Binding ElementName=HtmlRichTextBox, Converter={StaticResource ResourceKey=RichTextBoxContentConverter}, Mode=OneWay}"/>
The eagle-eyed among you will note that I am using a ValueConverter to convert the contents of the RichTextBox into text that is bound to the TextBox's Text property. The code for the ValueConverter is here:
<ValueConversion(GetType(RichTextBox), GetType(String))> _
Public Class RichTextBoxContentConverter : Implements IValueConverter
Public Function Convert(value As Object, targetType As Type, parameter As Object, culture As Globalization.CultureInfo) As Object Implements IValueConverter.Convert
Dim returnValue As String = String.Empty
If TryCast(value, RichTextBox) IsNot Nothing Then
Dim rtb As RichTextBox = CType(value, RichTextBox)
Dim rtbTextRange As New TextRange(rtb.Document.ContentStart, rtb.Document.ContentEnd)
If Not rtbTextRange.IsEmpty Then returnValue = rtbTextRange.Text
End If
Return returnValue
End Function
Public Function ConvertBack(value As Object, targetType As Type, parameter As Object, culture As Globalization.CultureInfo) As Object Implements IValueConverter.ConvertBack
Throw New NotImplementedException()
End Function
End Class
The problem is that the TextBox Text property is set correctly when the View is first loaded but any subsequent changes to the contents of the RichTextBox do not update the TextBox.
If I use a simple example where I bind the Text property of one TextBox to another that works fine which suggests that perhaps it's the ValueConverter which is the cause of the problem. I am assuming that somewhere along the line I am not notifying the target of changes to the source property but I can't work out where I am going wrong.
Many thanks for any help.