2016-08-21 70 views
0

我在Windows 10 UWP应用程序中有一个TextBox,看起来像这样。如何在复制文本框中的格式化文本时粘贴值

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

我想要做的是在此TextBox中复制/粘贴一些文本。问题是,当我从电子邮件,网站或OneNote复制文本时,文本不会被粘贴。

但是,当我在记事本中粘贴该文本,并从那里复制到文本框,它的工作原理。

我认为这是因为文本包含格式并且TextBox不支持粘贴格式化文本。

有很多这样的问题,但他们涉及非常具体的解决方案和自定义粘贴事件处理程序。

如何从文本框中的格式化文本中粘贴文本?它需要一个自定义的Paste事件处理程序吗?

非常感谢。

+0

无法重现您的问题发现的例子的简化。我可以复制表单Mail,Edge和OneNote并粘贴到TextBox中。我建议你创建一个只有一个TextBox的空白项目再次测试。 –

+0

这很有趣,你有没有尝试将格式化文本复制到Wunderlist UWP应用程序?它有同样的问题。尽管设法找到解决方案。 –

回答

0

所以我为Paste事件创建了一个事件处理程序。基本上我所做的只是将文本从剪贴板复制到文本框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 
       } 
      } 
     } 
    } 
相关问题