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.

Venkat
  • 2,549
  • 2
  • 28
  • 61
  • Do I miss anything basic here, please suggest? – Venkat Jan 18 '18 at 08:53
  • 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? – Mike Strobel Jan 23 '18 at 15:25
  • Yes, I have handled the previewKeyDown and PreviewKeyUp event. Some logics are written in the methods. – Venkat Jan 24 '18 at 01:15
  • 1
    Can you share some code? – Gaurang Dave Jan 24 '18 at 07:17
  • 2
    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. – Mike Strobel Jan 24 '18 at 14:02

1 Answers1

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;
            }
        }
    }
}
Jason Stevenson
  • 4,004
  • 3
  • 29
  • 49