Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cursor position is not changed for up and down key in Avalon text editor

I am using the Avalon text editor and the cursor position is not changed for the key up and key down. Can anyone suggest me how to overcome this issue?

The cursor position properly changes for the key right and left navigations.

I have also checked the value of caret in the key-up event and it doesn't change for up and down key presses.

like image 858
Venkat Avatar asked Jan 18 '18 07:01

Venkat


1 Answers

As stated by @Mike-Strobel you need to mark the event args handled to false, otherwise the underlying TextEditor control will not receive the event. You should check your PreviewKeyDown event, the PreviewKeyUp event will not block cursor events.

private void TextEditor_PreviewKeyDown(object sender, System.Windows.Input.KeyEventArgs e)
{
    e.Handled = false;
}

If you are trying just to capture the text from the control, and are using an MVVM pattern, then you might want to look at a behavior I use to data bind to the the Text property. This allows you to eliminate code-behind.

<avalonedit:TextEditor
    x:Name="TextEditor"
    FontFamily="Consolas"
    FontSize="10pt"
    LineNumbersForeground="Silver"
    ShowLineNumbers="True"
    SyntaxHighlighting="XML"
    PreviewKeyDown="TextEditor_PreviewKeyDown"
    PreviewKeyUp="TextEditor_PreviewKeyUp"
    WordWrap="True">
    <i:Interaction.Behaviors>
        <behaviors:AvalonEditTextBindingBehavior TextBinding="{Binding XamlEditText, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
    </i:Interaction.Behaviors>
</avalonedit:TextEditor>

You will need a couple namespaces in your xaml usercontrol or window:

xmlns:behaviors="clr-namespace:MatrixGold.Behaviors"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"

Here is the code for the behavior:

using System;
using System.Windows;
using System.Windows.Interactivity;
using ICSharpCode.AvalonEdit;

namespace MyProject.Behaviors
{
    public sealed class AvalonEditTextBindingBehavior : Behavior<TextEditor>
    {
        public static readonly DependencyProperty TextBindingProperty = DependencyProperty.Register("TextBinding", typeof(string), typeof(AvalonEditTextBindingBehavior), new FrameworkPropertyMetadata(default(string), FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, PropertyChangedCallback));

        private static void PropertyChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
        {
            var behavior = dependencyObject as AvalonEditTextBindingBehavior;
            if (behavior.AssociatedObject != null)
            {
                var editor = behavior.AssociatedObject;
                if (editor.Document != null
                    && dependencyPropertyChangedEventArgs.NewValue != null)
                {
                    // Update Text from Binding
                    editor.Document.Text = dependencyPropertyChangedEventArgs.NewValue.ToString();

                    // If Pasting In, Scroll to End of Content just Inserted
                    var caretOffset = editor.CaretOffset;
                    if (caretOffset > editor.Document.TextLength)
                    {
                        caretOffset = editor.Document.TextLength;
                        editor.CaretOffset = caretOffset;
                    }
                }
                else
                {
                    editor.Document.Text = string.Empty;
                }
            }
        }

        public string TextBinding
        {
            get { return (string) GetValue(TextBindingProperty); }
            set { SetValue(TextBindingProperty, value); }
        }

        protected override void OnAttached()
        {
            base.OnAttached();
            if (AssociatedObject != null)
                AssociatedObject.TextChanged += AssociatedObjectOnTextChanged;
        }

        protected override void OnDetaching()
        {
            base.OnDetaching();
            if (AssociatedObject != null)
                AssociatedObject.TextChanged -= AssociatedObjectOnTextChanged;
        }

        private void AssociatedObjectOnTextChanged(object sender, EventArgs eventArgs)
        {
            var textEditor = sender as TextEditor;
            if (textEditor != null)
            {
                if (textEditor.Document != null)
                    TextBinding = textEditor.Document.Text;
            }
        }
    }
}
like image 87
Jason Stevenson Avatar answered Sep 30 '22 14:09

Jason Stevenson