سؤال

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.

هل كانت مفيدة؟

المحلول 3

You can use it as you originally wanted. It only needs to be updated explicit. I share my implementation with you. I tried it and it worked. Sorry but I did it in C# not in VB.

XAML:

    <RichTextBox Name="HtmlRichTextBox" TextChanged="HtmlRichTextBox_OnTextChanged">

The event handler in code-behind:

        if (this.MyTextBox != null)
        {
            BindingExpression bindingExpr = this.MyTextBox.GetBindingExpression(TextBox.TextProperty);
            bindingExpr.UpdateTarget();
        }

MyTextBox is the name of the textbox, which is bound to RichTextBox. I hope it could help you.

نصائح أخرى

You are binding to the element HtmlRichTextBox, not it's content. Since that element is never changing to a different instance, it never updates and your converter is never called.

In order to get the behavior you expect, you would need to bind to its content rather than the actual element (since the element itself is never changing, only its content is). That can actually be problematic with the RichTextBox control, but there are solutions: See Richtextbox wpf binding.

Your binding is to the RichTextBox itself, which never changes. You'll need to bind to the thing to which changes should trigger your binding to update.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top