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

1

9

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.

Elledge answered 18/1, 2018 at 7:29 Comment(5)
Do I miss anything basic here, please suggest?Elledge
Are you processing the PreviewKeyDown event somewhere in your window? If so, are you marking the event as Handled? Is the "Scroll Lock" button toggled on your keyboard?Biologist
Yes, I have handled the previewKeyDown and PreviewKeyUp event. Some logics are written in the methods.Elledge
Can you share some code?Joline
If you are processing PreviewKeyDown, then you are intercepting keystrokes before they get to the AvalonEdit control. This isn't necessarily wrong, but if you set the event's Handled property to true, then you will prevent the editor from handling the same event. Consequently, it may not see certain keystrokes. It really depends on your key handlers. We'll need to see some code.Biologist
A
1

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;
            }
        }
    }
}
Annunciation answered 23/6, 2018 at 14:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.