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

설명

붙여넣기 이벤트는 콘텐츠가 컨트롤에 삽입되기 전에 발생합니다. 이 이벤트를 처리하여 클립보드의 내용을 검사 삽입하기 전에 콘텐츠에 대한 작업을 수행할 수 있습니다. 작업을 수행하는 경우 이벤트 인수의 Handled 속성을 true로 설정합니다. 그렇지 않으면 기본 붙여넣기 작업이 수행됩니다. 이벤트를 처리된 것으로 표시하면 앱이 붙여넣기 작업을 처리했으며 기본 작업이 수행되지 않는다고 가정합니다. 삽입할 삽입 지점 및 클립보드 콘텐츠를 확인하고 콘텐츠를 삽입해야 합니다.

사용자 지정 붙여넣기 작업을 수행하려면 코드 앞에 Handled 속성을 처리기에서 true 로 설정해야 합니다. 그렇지 않으면 기본 붙여넣기 작업이 수행된 다음 사용자 지정 작업이 수행됩니다. 사용자는 TextBox에서 콘텐츠가 변경된 것을 볼 수 있습니다.

적용 대상