TextBox.Paste Ereignis

Definition

Tritt auf, wenn Text in das Steuerelement eingefügt wird.

public:
 virtual event TextControlPasteEventHandler ^ Paste;
// Register
event_token Paste(TextControlPasteEventHandler const& handler) const;

// Revoke with event_token
void Paste(event_token const* cookie) const;

// Revoke with event_revoker
TextBox::Paste_revoker Paste(auto_revoke_t, TextControlPasteEventHandler const& handler) const;
public event TextControlPasteEventHandler Paste;
function onPaste(eventArgs) { /* Your code */ }
textBox.addEventListener("paste", onPaste);
textBox.removeEventListener("paste", onPaste);
- or -
textBox.onpaste = onPaste;
Public Custom Event Paste As TextControlPasteEventHandler 
<TextBox Paste="eventhandler"/>

Ereignistyp

Beispiele

In diesem Beispiel wird gezeigt, wie das Paste-Ereignis behandelt wird, um Zeilenumbrüche durch Kommas zu ersetzen, wenn sie in ein Adressfeld eingefügt werden. Andernfalls würde das Einfügen einer aus mehreren Zeilen kopierten Adresse zu Datenverlust führen.

<TextBox Header="Address" Paste="AddressTextBox_Paste"/>
private async void AddressTextBox_Paste(object sender, TextControlPasteEventArgs e)
{
    TextBox addressBox = sender as TextBox;
    if (addressBox != null)
    {
        // Mark the event as handled first. Otherwise, the
        // default paste action will happen, then the custom paste
        // action, and the user will see the text box content change.
        e.Handled = true;

        // Get content from the clipboard.
        var dataPackageView = Windows.ApplicationModel.DataTransfer.Clipboard.GetContent();
        if (dataPackageView.Contains(Windows.ApplicationModel.DataTransfer.StandardDataFormats.Text))
        {
            try
            {
                var text = await dataPackageView.GetTextAsync();

                // Remove line breaks from multi-line text and
                // replace with comma(,).
                string singleLineText = text.Replace("\r\n", ", ");

                // Replace any text currently in the text box.
                addressBox.Text = singleLineText;
            }
            catch (Exception)
            {
                // Ignore or handle exception as needed.
            }
        }
    }
}

In diesem Beispiel wird gezeigt, wie das Paste-Ereignis behandelt wird, um die Anzahl der in das Textfeld eingefügten Zeichen zu begrenzen. Wenn die Länge des Texts in der Zwischenablage die MaxLength des TextBox-Werts überschreitet, wird dem Benutzer eine Meldung angezeigt. Der Benutzer hat die Möglichkeit, mit dem abgeschnittenen Text fortzufahren oder den Einfügevorgang abzubrechen.

<TextBox Paste="TextBox_Paste" MaxLength="10"/>
private async void TextBox_Paste(object sender, TextControlPasteEventArgs e)
{
    TextBox tb = sender as TextBox;
    if (tb != null)
    {
        // Mark the event as handled first. Otherwise, the
        // default paste action will happen, then the custom paste
        // action, and the user will see the text box content change.
        e.Handled = true;

        // Get content from the clipboard.
        var dataPackageView = Windows.ApplicationModel.DataTransfer.Clipboard.GetContent();
        if (dataPackageView.Contains(Windows.ApplicationModel.DataTransfer.StandardDataFormats.Text))
        {
            try
            {
                var text = await dataPackageView.GetTextAsync();
                if (text.Length > tb.MaxLength)
                {
                    // Pasted text is too long. Show a message to the user.
                    // Create the message dialog and set its content
                    var messageDialog = 
                        new Windows.UI.Popups.MessageDialog(
                            "Pasted text excedes maximum allowed (" 
                            + tb.MaxLength.ToString() + 
                            " characters). The text will be truncated.");

                    // Add commands to the message dialog.
                    messageDialog.Commands.Add(new UICommand("Continue", (command) =>
                    {
                        // Truncate the text to be pasted to the MaxLength of the text box.
                        string truncatedText = text.Substring(0, tb.MaxLength);
                        tb.Text = truncatedText;
                    }));
                    messageDialog.Commands.Add(new UICommand("Cancel", (command) =>
                    {
                        // Cancelled. Do nothing.
                    }));

                    // Set the command that will be invoked by default.
                    messageDialog.DefaultCommandIndex = 0;

                    // Set the command to be invoked when escape is pressed.
                    messageDialog.CancelCommandIndex = 1;

                    // Show the message dialog.
                    await messageDialog.ShowAsync();
                }
                else
                {
                    tb.Text = text;
                }                          
            }
            catch (Exception)
            {
                // Ignore or handle exception as needed.
            }
        }
    }
}

Hinweise

Das Paste-Ereignis tritt auf, bevor Inhalte in das Steuerelement eingefügt werden. Sie können dieses Ereignis behandeln, um den Inhalt der Zwischenablage zu überprüfen und alle Aktionen für den Inhalt auszuführen, bevor er eingefügt wird. Wenn Sie eine Aktion ausführen, legen Sie die Handled-Eigenschaft der Ereignisargumente auf true fest. Andernfalls wird die Standardmäßige Einfügeaktion ausgeführt. Wenn Sie das Ereignis als behandelt markieren, wird davon ausgegangen, dass die App den Einfügevorgang verarbeitet hat, und es wird keine Standardaktion ausgeführt. Sie sind dafür verantwortlich, die Einfügemarke und den einzufügenden Inhalt der Zwischenablage zu bestimmen und den Inhalt einzufügen.

Sie sollten die Handled-Eigenschaft in Ihrem Handler vor dem Code auf true festlegen, um eine benutzerdefinierte Einfügeaktion auszuführen. Andernfalls wird die Standardmäßige Einfügeaktion ausgeführt, und dann wird die benutzerdefinierte Aktion ausgeführt. Der Benutzer kann sehen, wie sich der Inhalt im Textfeld ändert.

Gilt für: