1

I am trying to create a RichTextBox with a Run object inside that has it's Text property bound to a string property in my custom-made object. Here's how I create the binding:

Paragraph paragraph = new Paragraph();
Run run = new Run();
Binding bodyBinding = new Binding("Body");
PresentationTraceSources.SetTraceLevel(bodyBinding, PresentationTraceLevel.High);
bodyBinding.Source = OpenedFiles[index];
bodyBinding.Mode = BindingMode.TwoWay;
bodyBinding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
run.SetBinding(Run.TextProperty, bodyBinding);

paragraph.Inlines.Add(run);
FlowDocument flowDocument = new FlowDocument(paragraph);
RichTextBox rtb = new RichTextBox(flowDocument);

This is the debugging info when creating the binding:

System.Windows.Data Warning: 56  : Created BindingExpression (hash=27037160) for Binding (hash=32830290)
System.Windows.Data Warning: 58  : Path: 'Body'
System.Windows.Data Warning: 62  : BindingExpression (hash=27037160): Attach to System.Windows.Documents.Run.Text (hash=42007851)
System.Windows.Data Warning: 67  : BindingExpression (hash=27037160): Resolving source 
System.Windows.Data Warning: 70  : BindingExpression (hash=27037160): Found data context element: <null> (OK)
System.Windows.Data Warning: 78  : BindingExpression (hash=27037160): Activate with root item OpenedFile (hash=42526340)
System.Windows.Data Warning: 108 : BindingExpression (hash=27037160):   At level 0 - for OpenedFile.Body found accessor RuntimePropertyInfo(Body)
System.Windows.Data Warning: 104 : BindingExpression (hash=27037160): Replace item at level 0 with OpenedFile (hash=42526340), using accessor RuntimePropertyInfo(Body)
System.Windows.Data Warning: 101 : BindingExpression (hash=27037160): GetValue at level 0 from OpenedFile (hash=42526340) using RuntimePropertyInfo(Body): 'nikjodfewsanioudawnuidwanuidwniuainudwsodf'
System.Windows.Data Warning: 80  : BindingExpression (hash=27037160): TransferValue - got raw value 'nikjodfewsanioudawnuidwanuidwniuainudwsodf'
System.Windows.Data Warning: 89  : BindingExpression (hash=27037160): TransferValue - using final value 'nikjodfewsanioudawnuidwanuidwniuainudwsodf'

And this is how the OpenedFile class looks like:

public class OpenedFile : INotifyPropertyChanged
{
    //Implementation of INotifyPropertyChanged interface 
    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    private string _name;
    private string _body;
    private string _prevbody;
    private string _path;
    private bool _modified;

    public string Name { get { return _name; } set { _name = value; NotifyPropertyChanged();} }
    public string Body {
        get { return _body; }
        set {
                _body = value;
                NotifyPropertyChanged();
            }
    }
    public string Path { get { return _path; } set { _path = value; NotifyPropertyChanged();} }
    public bool Modified { get { return _modified; } set { _modified = value; NotifyPropertyChanged(); } }
    public OpenedFile(string name, string body, string path)
    {
        Name = name;
        Body = body;
        Path = path;
        Modified = false;
    }

    public OpenedFile()
    {
        Name = "New File";
        Body = "";
        Path = "";
        Modified = true;
    }
}

This binding works at first (the RichTextBox displays the correct file's contents when it is created), but as soon as I type something into the RichTextBox, it gives me following info (in this example, I typed 'x' at the end of the RichTextBox):

System.Windows.Data Warning: 90  : BindingExpression (hash=27037160): Update - got raw value 'nikjodfewsanioudawnuidwanuidwniuainudwsodfx'
System.Windows.Data Warning: 94  : BindingExpression (hash=27037160): Update - using final value 'nikjodfewsanioudawnuidwanuidwniuainudwsodfx'
System.Windows.Data Warning: 102 : BindingExpression (hash=27037160): SetValue at level 0 to OpenedFile (hash=42526340) using RuntimePropertyInfo(Body): 'nikjodfewsanioudawnuidwanuidwniuainudwsodfx'
System.Windows.Data Warning: 95  : BindingExpression (hash=27037160): Got PropertyChanged event from OpenedFile (hash=42526340)
System.Windows.Data Warning: 101 : BindingExpression (hash=27037160): GetValue at level 0 from OpenedFile (hash=42526340) using RuntimePropertyInfo(Body): 'nikjodfewsanioudawnuidwanuidwniuainudwsodfx'
System.Windows.Data Warning: 80  : BindingExpression (hash=27037160): TransferValue - got raw value 'nikjodfewsanioudawnuidwanuidwniuainudwsodfx'
System.Windows.Data Warning: 89  : BindingExpression (hash=27037160): TransferValue - using final value 'nikjodfewsanioudawnuidwanuidwniuainudwsodfx'
System.Windows.Data Warning: 90  : BindingExpression (hash=27037160): Update - got raw value 'nikjodfewsanioudawnuidwanuidwniuainudwsodf'
System.Windows.Data Warning: 94  : BindingExpression (hash=27037160): Update - using final value 'nikjodfewsanioudawnuidwanuidwniuainudwsodf'
System.Windows.Data Warning: 102 : BindingExpression (hash=27037160): SetValue at level 0 to OpenedFile (hash=42526340) using RuntimePropertyInfo(Body): 'nikjodfewsanioudawnuidwanuidwniuainudwsodf'
System.Windows.Data Warning: 95  : BindingExpression (hash=27037160): Got PropertyChanged event from OpenedFile (hash=42526340)
System.Windows.Data Warning: 101 : BindingExpression (hash=27037160): GetValue at level 0 from OpenedFile (hash=42526340) using RuntimePropertyInfo(Body): 'nikjodfewsanioudawnuidwanuidwniuainudwsodf'
System.Windows.Data Warning: 80  : BindingExpression (hash=27037160): TransferValue - got raw value 'nikjodfewsanioudawnuidwanuidwniuainudwsodf'
System.Windows.Data Warning: 89  : BindingExpression (hash=27037160): TransferValue - using final value 'nikjodfewsanioudawnuidwanuidwniuainudwsodf'

So, it first correctly catches the change and updates it, but then reverts it back to the old value. Moreover, after that happens, the Binding completely breaks - it doesn't update on any text input, but it also doesn't display any info in debugging.

Does anyone know how to fix it? Please, I'm desperate at this point, I've been trying to debug it for hours. I tried removing the INotifyPropertyChanged implementation from OpenedFile class and setting the binding to a different mode.

Mike
  • 850
  • 10
  • 33
  • 1
    Maybe, as a workaround, it would be enough to change the binding to OneWay? – Maciek Świszczowski May 28 '18 at 10:55
  • I tried to change it to `OneWayToSource`, and set the Run.Text property manually after setting the binding. `OneWay` wouldn't work, because in this case the priority is to keep `OpenedFile` object updated. Unfortunately, it gives me the exact same result. It feels like a curse, really. – Alicja Barankiewicz May 28 '18 at 10:57
  • 1
    `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.` from [Bindable Run](https://blogs.msdn.microsoft.com/text/2009/09/01/bindable-run/) – FoggyFinder May 28 '18 at 11:09
  • oh my god... thank you for the information – Alicja Barankiewicz May 28 '18 at 11:17
  • 1
    or see this: https://stackoverflow.com/a/14655500/275330. In a comment: this will fail if the user does a SelectAll and Delete since that paragraph element will be removed. – Maciek Świszczowski May 28 '18 at 11:21

0 Answers0