/// <summary> /// Event handler for ApplicationCommands.Paste command. /// <remarks> /// We want to allow paste only in plain text format. /// </remarks> /// </summary> private static void OnPaste(object sender, ExecutedRoutedEventArgs e) { TextOnlyRichTextBox TextOnlyRichTextBox = (TextOnlyRichTextBox)sender; // Handle paste only if clipboard supports text format. if (Clipboard.ContainsText()) { TextOnlyRichTextBox.Selection.Text = Clipboard.GetText(); } e.Handled = true; }
/// <summary> /// Event handler for ApplicationCommands.Copy command. /// <remarks> /// We want to enforce that data can be set on the clipboard /// only in plain text format from this RichTextBox. /// </remarks> /// </summary> private static void OnCopy(object sender, ExecutedRoutedEventArgs e) { TextOnlyRichTextBox TextOnlyRichTextBox = (TextOnlyRichTextBox)sender; string selectionText = TextOnlyRichTextBox.Selection.Text; var range = new TextRange(TextOnlyRichTextBox.Selection.Start, TextOnlyRichTextBox.Selection.End); if (String.IsNullOrEmpty(selectionText.Trim())) { } else { Clipboard.SetText(selectionText); } e.Handled = true; }
/// <summary> /// Event handler for ApplicationCommands.Cut command. /// <remarks> /// We want to enforce that data can be set on the clipboard /// only in plain text format from this RichTextBox. /// </remarks> /// </summary> private static void OnCut(object sender, ExecutedRoutedEventArgs e) { TextOnlyRichTextBox TextOnlyRichTextBox = (TextOnlyRichTextBox)sender; string selectionText = TextOnlyRichTextBox.Selection.Text; if (String.IsNullOrEmpty(selectionText.Trim())) { } else { Clipboard.SetText(selectionText); } TextOnlyRichTextBox.Selection.Text = String.Empty; e.Handled = true; }
/// <summary> /// CanExecute event handler for ApplicationCommand.Paste. /// </summary> private static void OnCanExecutePaste(object target, CanExecuteRoutedEventArgs args) { TextOnlyRichTextBox TextOnlyRichTextBox = (TextOnlyRichTextBox)target; args.CanExecute = TextOnlyRichTextBox.IsEnabled && !TextOnlyRichTextBox.IsReadOnly && Clipboard.ContainsText(); }
/// <summary> /// CanExecute event handler for ApplicationCommands.Cut. /// </summary> private static void OnCanExecuteCut(object target, CanExecuteRoutedEventArgs args) { TextOnlyRichTextBox TextOnlyRichTextBox = (TextOnlyRichTextBox)target; args.CanExecute = TextOnlyRichTextBox.IsEnabled && !TextOnlyRichTextBox.IsReadOnly && !TextOnlyRichTextBox.Selection.IsEmpty; }