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;
}
}
}
}
PreviewKeyDown
event somewhere in your window? If so, are you marking the event asHandled
? Is the "Scroll Lock" button toggled on your keyboard? – BiologistPreviewKeyDown
, then you are intercepting keystrokes before they get to theAvalonEdit
control. This isn't necessarily wrong, but if you set the event'sHandled
property totrue
, 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