Question

I'm trying to get AvalonEdit with MVVM Pattern to working, but I don't really know what to do exactly. I want to bind the SelectionLength and the SelectionStart to my ViewModel so I can access these two values when I execute some business logic.

I started to create DependencyProperties like that:

public class MvvmTextEditor : TextEditor, INotifyPropertyChanged
{
    /// <summary>
    /// A bindable Text property
    /// </summary>
    public new string Text
    {
        get { return base.Text; }
        set { 
            base.Text = value; 
            RaisePropertyChanged();
        }
    }

    /// <summary>
    /// A bindable SelectionStart property
    /// </summary>
    public new int SelectionStart
    {
        get { return base.SelectionStart; }
        set {base.SelectionStart = value; }
    }

    /// <summary>
    /// A bindable SelectionLength property
    /// </summary>
    public new int SelectionLength
    {
        get { return base.SelectionLength; }
        set { base.SelectionLength = value; }
    }

    /// <summary>
    /// The bindable selection start property dependency property
    /// </summary>
    public static readonly DependencyProperty SelectionStartProperty =
        DependencyProperty.Register("SelectionStart", typeof(int), typeof(MvvmTextEditor),
            new PropertyMetadata((o, args) =>
            {
                var target = (TextEditor)o;
                target.SelectionStart = (int)args.NewValue;
            }));

    /// <summary>
    /// The bindable selection length property dependency property
    /// </summary>
    public static readonly DependencyProperty SelectionLengthProperty =
        DependencyProperty.Register("SelectionLength", typeof(int), typeof(MvvmTextEditor),
            new PropertyMetadata((o, args) =>
            {
                var target = (MvvmTextEditor) o;

                target.SelectionLength = (int)args.NewValue;
                Debug.WriteLine(target.SelectionLength);

            }));

    /// <summary>
    /// The bindable text property dependency property
    /// </summary>
    public static readonly DependencyProperty TextProperty =
        DependencyProperty.Register("Text", typeof(string), typeof(MvvmTextEditor),
            new PropertyMetadata((o, args) =>
            {
                var target = (MvvmTextEditor)o;
                target.Text = (string)args.NewValue;
            }));

    /// <summary>
    /// Raises a property changed event
    /// </summary>
    /// <param name="property">The name of the property that updates</param>
    public void RaisePropertyChanged([CallerMemberName] string caller = "")
    {
        var handler = PropertyChanged;
        if (handler != null)
            PropertyChanged(this, new PropertyChangedEventArgs(caller));
    }

    public event PropertyChangedEventHandler PropertyChanged;
}

TextDependencyProperty is working fine but SelectionLength and SelectionStart is not working.

I've added an eventhandler for SelectionChanged (but I don't exactly what I'm doing here with SetValue:

    public MvvmTextEditor()
    {
        TextArea.SelectionChanged += TextArea_SelectionChanged;
    }

    void TextArea_SelectionChanged(object sender, EventArgs e)
    {
        SetValue(SelectionStartProperty, SelectionStart);
        SetValue(SelectionLengthProperty, SelectionLength);
    }

Selection is working now, but there is a problem with doing selections backwards. In this case the SelectionStart is always 0. If everything is correct what I'm done so far, then I would create a logic, that converts index and length correct if someone selects backwards. Do I have to implement this logic in the PropertyMetaDataDelegate?

No correct solution

OTHER TIPS

Right, the reason for this was clear once I looked in to it. In the setter of the property that backed the DP for both SelectionStart and SelectionEnd we were setting base.SelectionStart and base.SelectionLength this was creating a circular update of these values and prevented the correct update of theses values from the base class. The updated MVVM TextEditor class is below.

/// <summary>
/// Class that inherits from the AvalonEdit TextEditor control to 
/// enable MVVM interaction. 
/// </summary>
public class MvvmTextEditor : TextEditor, INotifyPropertyChanged
{
    /// <summary>
    /// Default constructor to set up event handlers.
    /// </summary>
    public MvvmTextEditor()
    {
        TextArea.SelectionChanged += TextArea_SelectionChanged;
    }

    /// <summary>
    /// Event handler to update properties based upon the selection changed event.
    /// </summary>
    void TextArea_SelectionChanged(object sender, EventArgs e)
    {
        this.SelectionStart = SelectionStart;
        this.SelectionLength = SelectionLength;
    }

    #region Text.
    /// <summary>
    /// Dependancy property for the editor text property binding.
    /// </summary>
    public static readonly DependencyProperty TextProperty =
         DependencyProperty.Register("Text", typeof(string), typeof(MvvmTextEditor),
         new PropertyMetadata((obj, args) =>
         {
             MvvmTextEditor target = (MvvmTextEditor)obj;
             target.Text = (string)args.NewValue;
         }));

    /// <summary>
    /// Provide access to the Text.
    /// </summary>
    public new string Text
    {
        get { return base.Text; }
        set { base.Text = value; }
    }

    /// <summary>
    /// Return the current text length.
    /// </summary>
    public int Length
    {
        get { return base.Text.Length; }
    }

    /// <summary>
    /// Override of OnTextChanged event.
    /// </summary>
    protected override void OnTextChanged(EventArgs e)
    {
        RaisePropertyChanged("Length");
        base.OnTextChanged(e);
    }
    #endregion // Text.

    #region Caret Offset.
    /// <summary>
    /// DependencyProperty for the TextEditorCaretOffset binding. 
    /// </summary>
    public static DependencyProperty CaretOffsetProperty =
        DependencyProperty.Register("CaretOffset", typeof(int), typeof(MvvmTextEditor),
        new PropertyMetadata((obj, args) =>
        {
            MvvmTextEditor target = (MvvmTextEditor)obj;
            target.CaretOffset = (int)args.NewValue;
        }));

    /// <summary>
    /// Provide access to the CaretOffset.
    /// </summary>
    public new int CaretOffset
    {
        get { return base.CaretOffset; }
        set { base.CaretOffset = value; }
    }
    #endregion // Caret Offset.

    #region Selection.
    /// <summary>
    /// DependencyProperty for the TextEditor SelectionLength property. 
    /// </summary>
    public static readonly DependencyProperty SelectionLengthProperty =
         DependencyProperty.Register("SelectionLength", typeof(int), typeof(MvvmTextEditor),
         new PropertyMetadata((obj, args) =>
             {
                 MvvmTextEditor target = (MvvmTextEditor)obj;
                 target.SelectionLength = (int)args.NewValue;
             }));

    /// <summary>
    /// Access to the SelectionLength property.
    /// </summary>
    public new int SelectionLength
    {
        get { return base.SelectionLength; }
        set { SetValue(SelectionLengthProperty, value); }
    }

    /// <summary>
    /// DependencyProperty for the TextEditor SelectionStart property. 
    /// </summary>
    public static readonly DependencyProperty SelectionStartProperty =
         DependencyProperty.Register("SelectionStart", typeof(int), typeof(MvvmTextEditor),
         new PropertyMetadata((obj, args) =>
             {
                 MvvmTextEditor target = (MvvmTextEditor)obj;
                 target.SelectionStart = (int)args.NewValue;
             }));

    /// <summary>
    /// Access to the SelectionStart property.
    /// </summary>
    public new int SelectionStart
    {
        get { return base.SelectionStart; }
        set { SetValue(SelectionStartProperty, value); }
    }
    #endregion // Selection.

    /// <summary>
    /// Implement the INotifyPropertyChanged event handler.
    /// </summary>
    public event PropertyChangedEventHandler PropertyChanged;
    public void RaisePropertyChanged([CallerMemberName] string caller = null)
    {
        var handler = PropertyChanged;
        if (handler != null)
            PropertyChanged(this, new PropertyChangedEventArgs(caller));
    }
}

I hope this helps someone else.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top