Avalon文本编辑器中的向上和向下键不会更改光标位置

时间:2018-01-18 07:29:06

标签: .net wpf events caret avalonedit

我正在使用Avalon text editor,并且键向上和键入的光标位置未更改。任何人都可以建议我如何克服这个问题?

键左右导航正确地更改了光标位置。

我还在按键事件中检查了插入符号的值,并且在上下键按下时它没有变化。

1 个答案:

答案 0 :(得分:1)

如@ Mike-Strobel所述,您需要将处理的事件args标记为false,否则底层的TextEditor控件将不会接收该事件。您应该检查您的PreviewKeyDown事件,PreviewKeyUp事件不会阻止光标事件。

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

如果您只是尝试从控件中捕获文本,并且正在使用MVVM模式,那么您可能希望查看我用于将数据绑定到Text属性的行为。这使您可以消除代码隐藏。

<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>

您将需要在xaml用户控件或窗口中使用几个命名空间:

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

以下是行为代码:

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;
            }
        }
    }
}