TextBox.Paste 事件
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
當文字貼上到控制鍵時會發生。
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 的字元數量。 如果剪貼簿上的文字長度超過文字框的最大長度,使用者會看到訊息。 使用者可以選擇以截斷文字繼續,或取消貼上操作。
<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 事件發生在任何內容插入控制項之前。 你可以處理這個事件,檢查剪貼簿的內容,並在內容插入前執行任何操作。 如果你執行任何動作,請將事件 args 的 Handled 屬性設為 true;否則,則執行預設的貼上動作。 如果你把事件標記為已處理,就會假設應用程式已經處理了貼上操作,預設動作不會被執行。 你負責決定插入點和剪貼板內容,並自行插入內容。
你應該在程式碼執行自訂貼上動作前,在處理器裡把 Handled 屬性設 為 true 。 否則,先執行預設的貼上動作,接著執行自訂動作。 使用者可以看到內容在 文字框中變化。