Two-way binding in AvalonEdit doesn't work
Asked Answered
A

3

10

I have used AvalonEdit in my project that is based on WPF and MVVM. After reading this post I created the following class:

public class MvvmTextEditor : TextEditor, INotifyPropertyChanged
{
    public static DependencyProperty DocumentTextProperty =
        DependencyProperty.Register("DocumentText", 
                                    typeof(string), typeof(MvvmTextEditor),
        new PropertyMetadata((obj, args) =>
        {
            MvvmTextEditor target = (MvvmTextEditor)obj;
            target.DocumentText = (string)args.NewValue;
        })
    );

    public string DocumentText
    {
        get { return base.Text; }
        set { base.Text = value; }
    }

    protected override void OnTextChanged(EventArgs e)
    {
        RaisePropertyChanged("DocumentText");
        base.OnTextChanged(e);
    }

    public event PropertyChangedEventHandler PropertyChanged;
    public void RaisePropertyChanged(string info)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(info));
        }
    }
}

and used the following XAML to use this control:

<avalonedit:MvvmTextEditor x:Name="xmlMessage">
   <avalonedit:MvvmTextEditor.DocumentText>
      <Binding Path ="MessageXml" Mode="TwoWay" 
               UpdateSourceTrigger="PropertyChanged">
         <Binding.ValidationRules>
            <local:XMLMessageValidationRule />
          </Binding.ValidationRules>
      </Binding>
   </avalonedit:MvvmTextEditor.DocumentText>
</avalonedit:MvvmTextEditor>

but the binding works OneWay and doesn't update my string property nor run validation rule.

How can I fix binding to work as expected TwoWay?

Adorl answered 13/2, 2013 at 14:1 Comment(1)
Can you please tell me exactly how you got this working? I have asked another question here...Bohemia
S
8

WPF bindings do not use your DocumentText property; instead they access the underlying value of the dependency property directly.

Your OnTextChanged method doesn't actually change the value of the underlying dependency property. You will need to copy the value from base.Text into the dependency property on every change:

protected override void OnTextChanged(EventArgs e)
{
    SetCurrentValue(DocumentTextProperty, base.Text);
    base.OnTextChanged(e);
}

This issue would be easier to see if you followed the correct pattern for implementing DependencyProperty: The DocumentText property should use the GetValue/SetValue methods, and not access a different backing store.

Subdelirium answered 13/2, 2013 at 21:27 Comment(1)
Thanks very much for the reply - I has helped. I am however confused about how to use the GetValue/SetValue in the code above for MvvmTextEditor class? It seem that it is not compatible with the current implementation.Bohemia
M
4

Even using GetValue and SetValue you can't get the TextProperty to update the binded when the text change, so Daniel answer must be followed anyway.

I did change a bit to make it more intuitive to the end user having to use the Text as normal and dependecy mode:

    public new string Text
    {
        get { return (string)GetValue(TextProperty); }
        set { SetValue(TextProperty, value); }
    }

    internal string baseText { get { return base.Text; } set { base.Text = value; } }

    public static DependencyProperty TextProperty =
    DependencyProperty.Register("Text", typeof(string), typeof(MvvmTextEditor),
        // binding changed callback: set value of underlying property
        new PropertyMetadata((obj, args) =>
        {
            MvvmTextEditor target = (MvvmTextEditor)obj;
            if(target.baseText != (string)args.NewValue)    //avoid undo stack overflow
                target.baseText = (string)args.NewValue;
        })
    );

    protected override void OnTextChanged(EventArgs e)
    {            
        SetCurrentValue(TextProperty, baseText);
        RaisePropertyChanged("Text");
        base.OnTextChanged(e);
    }

I had to check if the same text was already there to avoid the undo stack engine exception. Also performance wise is a good deal.

Mendy answered 9/4, 2014 at 15:41 Comment(0)
A
2

I tried the code based on the answers above with slight modifications, as binding was not working for me both ways. The content below should allow to bind two ways.

    public static readonly DependencyProperty MyContentProperty = DependencyProperty.Register(
        "MyContent", typeof(string), typeof(MyTextEditor), new PropertyMetadata("", OnMyContentChanged));

    private static void OnMyContentChanged(object sender, DependencyPropertyChangedEventArgs e)
    {
        var control = (MyTextEditor)sender;
        if (string.Compare(control.MyContent, e.NewValue.ToString()) != 0)
        {
            //avoid undo stack overflow
            control.MyContent = e.NewValue.ToString();
        }
    }

    public string MyContent
    {
        get { return Text; }
        set { Text = value; }
    }

    protected override void OnTextChanged(EventArgs e)
    {
        SetCurrentValue(MyContentProperty, Text);
        base.OnTextChanged(e);
    }
Anhydrite answered 16/1, 2015 at 9:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.