简体   繁体   English

如何使用通用Windows应用程序中的拖放功能将文本从一个Textblock移至另一个Textblock?

[英]How to move text from one Textblock to another Textblock using Drag and Drop in a Universal Windows Application?

I know how to set properties like CanDrag and AllowDrop and define DragOver method and Drop method. 我知道如何设置CanDrag和AllowDrop之类的属性,以及定义DragOver方法和Drop方法。 I just don't know what to write inside the Drop method. 我只是不知道在Drop方法中写什么。

How to move text from one Textblock to another Textblock using Drag and Drop 如何使用拖放功能将文本从一个Textblock移至另一个Textblock

We can define DragStarting event for the source Textblock and save the text of the source Textblock in DragStartingEventArgs for transfer during dragging. 我们可以定义DragStarting为源事件Textblock ,并保存源的文字TextblockDragStartingEventArgs拖动过程中转移。 And accept the text when drop at target Textblock . 并在放置到目标Textblock时接受文本。 Read the text from DragEventHandler and set it to the target Textblock . DragEventHandler读取文本并将其设置为目标Textblock

I wrote a simple sample here, move the text from txtsource to append to txttarget . 我在这里写了一个简单的示例,将文本从txtsource追加到txttarget

XAML code: XAML代码:

<StackPanel Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" Padding="30">
    <Border BorderBrush="Azure" BorderThickness="2">
        <TextBlock x:Name="txtsource" Text="I'm the first textblock" CanDrag="True" DragStarting="txtsource_DragStarting"  />
    </Border>
    <Border BorderBrush="Azure" BorderThickness="2" Margin="20" AllowDrop="True" >
        <TextBlock x:Name="txttarget" Text="I'm the second textblock" Drop="txttarget_Drop"  Height="50" Width="400"  AllowDrop="True" DragEnter="txttarget_DragEnter"/>
    </Border>
</StackPanel>

Code behind 后面的代码

  private void txtsource_DragStarting(UIElement sender, DragStartingEventArgs args)
  {
      args.Data.SetText(txtsource.Text);
  }

  private async void txttarget_Drop(object sender, DragEventArgs e)
  {
      bool hasText = e.DataView.Contains(StandardDataFormats.Text);
      e.AcceptedOperation = hasText ? DataPackageOperation.Copy : DataPackageOperation.None;
      if (hasText)
      {
          var text = await e.DataView.GetTextAsync();
          txttarget.Text +="\n"+ text;
      }
  }
  private void txttarget_DragEnter(object sender, DragEventArgs e)
  {
      bool hasText = e.DataView.Contains(StandardDataFormats.Text);
      e.AcceptedOperation = hasText ? DataPackageOperation.Copy : DataPackageOperation.None;
      if (hasText)
      {
          e.DragUIOverride.Caption = "Drop here to insert text";
      }
  }

I use DragOver event to help define which area can be drop. 我使用DragOver事件来帮助定义可以放置的区域。 More details please reference the scenario 2 of the official sample . 更多详细信息,请参考官方样本的方案2。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果第一个文本块的文本长度受到限制,如何继续从一个文本块到另一个文本块的文本? - How to Continue The Text from one textblock to another textblock if the first textblock text length is limited? 将TextBlock从StackPanel顶部拖放到另一个 - Drag TextBlock from StackPanel top drop on another 在文本块中的文本上删除阴影 - Drop Shadow on text in textblock 如何在Windows Phone应用程序中将RichTextBox文本转换为文本块? - How to get richtextbox text into textblock in windows phone application? TextBlock边框使TextBlock移到另一个位置 - TextBlock border makes TextBlock move to another location 如何将文本块可见性绑定到另一个文本块文本属性 - How to bind a textblock visibility to another textblock text property 如何从一个视图访问TextBox中的文本,并在WPF C#中在另一个视图的TextBlock中显示文本 - How to access text from a TextBox from one view and display it in a TextBlock on another view in WPF C# 在winrt通用应用程序中将额外的文本附加到绑定的文本块 - Append extra text to a bound textblock in winrt universal application 从另一个页面更改文本块的文本 - Changing text of textblock from another page 如何获取TextBlock的文本(TextBlock是Button的内容) - How to get the Text of TextBlock (TextBlock is Content of Button)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM