共用方式為


TextBox.Paste 事件

定義

當文字貼到 控制項時發生。

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"/>

事件類型

範例

此範例示範如何在貼入位址欄位時,處理 Paste 事件,以逗號取代分行符號。 否則,貼上從多行複製的位址會導致資料遺失。

<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.
            }
        }
    }
}

此範例示範如何處理 Paste 事件,以限制貼到 TextBox中的字元數。 如果剪貼簿上的文字長度超過TextBoxMaxLength,則會向使用者顯示一則訊息。 使用者可以選擇繼續截斷文字,或取消貼上作業。

<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.
            }
        }
    }
}

備註

Paste 事件會在任何內容插入控制項之前發生。 您可以處理此事件,以檢查剪貼簿的內容,並在插入內容之前對內容執行任何動作。 如果您執行任何動作,請將事件引數的 Handled 屬性設定為 true;否則,會執行預設的貼上動作。 如果您將事件標示為已處理,則會假設應用程式已處理貼上作業,而且不會執行預設動作。 您必須負責判斷要插入的插入點和剪貼簿內容,以及插入內容。

您應該在程式碼執行自訂貼上動作之前,在處理常式中將 Handled 屬性設定為 true 。 否則,會執行預設貼上動作,然後執行自訂動作。 使用者可以在 TextBox中看到內容變更。

適用於