문제

I need a single-line AvalonEdit control (equivalent to TextBox with AcceptsReturn="False").

AvalonEdit does not seem to have this property.

How do I do this for AvalonEdit?

도움이 되었습니까?

해결책

You could try handling the PreviewKeyDown event and set e.Handled to true if the Key is Return.

In addition, I would guess that you want to prevent newlines being pasted into the text area. This would have to be done by the following thing:

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

다른 팁

Here's my Editor.TextArea.PreviewKeyDown handler:

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

You could also check the shift state for going to the "Previous" and use a ternary operator to select direction:

var shiftPressed = (args.KeyboardDevice.Modifiers & ModifierKeys.Shift) == ModifierKeys.Shift;
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top