[英]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
,并保存源的文字Textblock
中DragStartingEventArgs
拖动过程中转移。 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.