在 TextBox 中复制格式化文本时如何粘贴值
How to paste value when copying formatted text in TextBox
我在 Windows 10 UWP 应用程序中有一个文本框,看起来像这样。
<TextBox Name="QuoteBox"
MinHeight="160"
TextAlignment="Left"
TextWrapping="Wrap"
Margin="12"
RelativePanel.AlignTopWithPanel="True"
RelativePanel.AlignRightWithPanel="True"
RelativePanel.AlignLeftWithPanel="True"
IsTabStop="True" KeyDown="InputBox_KeyDown"
Height="{x:Bind MainScrollViewer.ViewportHeight, Converter={StaticResource TwoFifthsConverter}, Mode=OneWay}" />
我想做的是在此文本框中复制/粘贴一些文本。问题是当我从电子邮件、网站甚至 OneNote 复制文本时,文本没有粘贴。
但是当我将该文本粘贴到记事本中,并将其从那里复制到 TextBox 时,它起作用了。
我认为这是因为文本包含格式,而 TextBox 不支持粘贴格式文本。
有很多这样的问题,但它们都涉及非常具体的解决方案和自定义粘贴事件处理程序。
如何从 TextBox 的格式化文本中粘贴文本?它是否需要自定义粘贴事件处理程序?
非常感谢。
所以我为粘贴事件创建了一个事件处理程序。基本上我所做的只是将文本从剪贴板复制到文本框 Text 属性.
它是粘贴事件处理程序中示例的简化 documentation page
/// <summary>
/// Used to paste text when copying formatted text
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private async void QuoteBox_Paste(object sender, TextControlPasteEventArgs e)
{
TextBox quoteBox = sender as TextBox;
if (quoteBox != 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.
DataPackageView dataPackageView = Clipboard.GetContent();
if(dataPackageView.Contains(StandardDataFormats.Text))
{
try
{
// Copy text from the clipboard
quoteBox.Text = await dataPackageView.GetTextAsync();
}
catch
{
// Ignore exception
}
}
}
}
我在 Windows 10 UWP 应用程序中有一个文本框,看起来像这样。
<TextBox Name="QuoteBox"
MinHeight="160"
TextAlignment="Left"
TextWrapping="Wrap"
Margin="12"
RelativePanel.AlignTopWithPanel="True"
RelativePanel.AlignRightWithPanel="True"
RelativePanel.AlignLeftWithPanel="True"
IsTabStop="True" KeyDown="InputBox_KeyDown"
Height="{x:Bind MainScrollViewer.ViewportHeight, Converter={StaticResource TwoFifthsConverter}, Mode=OneWay}" />
我想做的是在此文本框中复制/粘贴一些文本。问题是当我从电子邮件、网站甚至 OneNote 复制文本时,文本没有粘贴。
但是当我将该文本粘贴到记事本中,并将其从那里复制到 TextBox 时,它起作用了。
我认为这是因为文本包含格式,而 TextBox 不支持粘贴格式文本。
有很多这样的问题,但它们都涉及非常具体的解决方案和自定义粘贴事件处理程序。
如何从 TextBox 的格式化文本中粘贴文本?它是否需要自定义粘贴事件处理程序?
非常感谢。
所以我为粘贴事件创建了一个事件处理程序。基本上我所做的只是将文本从剪贴板复制到文本框 Text 属性.
它是粘贴事件处理程序中示例的简化 documentation page
/// <summary>
/// Used to paste text when copying formatted text
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private async void QuoteBox_Paste(object sender, TextControlPasteEventArgs e)
{
TextBox quoteBox = sender as TextBox;
if (quoteBox != 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.
DataPackageView dataPackageView = Clipboard.GetContent();
if(dataPackageView.Contains(StandardDataFormats.Text))
{
try
{
// Copy text from the clipboard
quoteBox.Text = await dataPackageView.GetTextAsync();
}
catch
{
// Ignore exception
}
}
}
}