2012-04-20 62 views

回答

3

如果密钥为返回,您可以尝试处理PreviewKeyDown事件并将e.Handled设置为true。

另外,我猜想你想防止换行符被粘贴到文本区域。这必须通过以下的事情要做:

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

这里是我的Editor.TextArea.PreviewKeyDown处理程序:

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

你也可以查看移动状态换去的“过去”和使用选择方向的三元运算符:

var shiftPressed = (args.KeyboardDevice.Modifiers & ModifierKeys.Shift) == ModifierKeys.Shift;