Ven*_*kat 9 .net wpf events caret avalonedit
我正在使用,Avalon text editor并且键和音频位置没有因按键向上和按键而改变.任何人都可以建议我如何克服这个问题?
键右右导航的光标位置正确变化.
我还检查了键盘事件中插入符号的值,并且对于上下键按下它没有改变.
正如 @Mike-Strobel 所说,您需要将处理的事件参数标记为 false,否则底层 TextEditor 控件将不会接收该事件。您应该检查您的 PreviewKeyDown 事件,PreviewKeyUp 事件不会阻止光标事件。
private void TextEditor_PreviewKeyDown(object sender, System.Windows.Input.KeyEventArgs e)
{
e.Handled = false;
}
Run Code Online (Sandbox Code Playgroud)
如果您只是尝试从控件捕获文本,并且使用 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>
Run Code Online (Sandbox Code Playgroud)
您的 xaml 用户控件或窗口中将需要几个命名空间:
xmlns:behaviors="clr-namespace:MatrixGold.Behaviors"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
Run Code Online (Sandbox Code Playgroud)
以下是该行为的代码:
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;
}
}
}
}
Run Code Online (Sandbox Code Playgroud)