首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AvalonEdit的AcceptsReturn=为“False”

AvalonEdit的AcceptsReturn=为“False”
EN

Stack Overflow用户
提问于 2012-04-20 09:03:41
回答 2查看 975关注 0票数 1

我需要一个单行的AvalonEdit控件(相当于AcceptsReturn=为“False”的TextBox )。

AvalonEdit似乎没有这个属性。

如何为AvalonEdit执行此操作?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-04-20 19:15:12

您可以尝试处理PreviewKeyDown事件,如果键是Return,则将e.Handled设置为true。

此外,我猜测您希望防止将换行符粘贴到文本区。这必须通过以下方式来完成:

代码语言:javascript
复制
void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
    // Find the Paste command of the avalon edit
    foreach (var commandBinding in textEditor.TextArea.CommandBindings.Cast<CommandBinding>())
    {
        if (commandBinding.Command == ApplicationCommands.Paste)
        {
            // Add a custom PreviewCanExecute handler so we can filter out newlines
            commandBinding.PreviewCanExecute += new CanExecuteRoutedEventHandler(pasteCommandBinding_PreviewCanExecute);
            break;
        }
    }
}

void pasteCommandBinding_PreviewCanExecute(object sender, CanExecuteRoutedEventArgs e)
{
    // Get clipboard data and stuff
    var dataObject = Clipboard.GetDataObject();
    var text = (string)dataObject.GetData(DataFormats.UnicodeText);
    // normalize newlines so we definitely get all the newlines
    text = TextUtilities.NormalizeNewLines(text, Environment.NewLine);

    // if the text contains newlines - replace them and paste again :)
    if (text.Contains(Environment.NewLine))
    {
        e.CanExecute = false;
        e.Handled = true;
        text = text.Replace(Environment.NewLine, " ");
        Clipboard.SetText(text);
        textEditor.Paste();
    }
}
票数 3
EN

Stack Overflow用户

发布于 2012-08-17 05:05:00

下面是我的Editor.TextArea.PreviewKeyDown处理程序:

代码语言:javascript
复制
    private void TabToOkayBtn(object sender, KeyEventArgs args)
    {
        if (args.Key == Key.Tab)
        {
            args.Handled = true;
            Dispatcher.BeginInvoke(DispatcherPriority.Input, new Action(() => // input priority is always needed when changing focus
                _editor.TextArea.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next))));
        }
    }

您还可以检查换档状态以转到“上一个”,并使用三元运算符选择方向:

代码语言:javascript
复制
var shiftPressed = (args.KeyboardDevice.Modifiers & ModifierKeys.Shift) == ModifierKeys.Shift;
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10238921

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档