前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >win10 uwp 如何拖动一个TextBlock的文字到另一个TextBlock

win10 uwp 如何拖动一个TextBlock的文字到另一个TextBlock

作者头像
林德熙
发布2018-09-18 15:59:38
4840
发布2018-09-18 15:59:38
举报
文章被收录于专栏:林德熙的博客

我在堆栈网看到有人问 如何拖动一个TextBlock的文字到另一个TextBlock 于是看到一个大神给出的方法,下面我就来和大家说下如何拖动

一开始我们需要一个界面,就放两个TextBlock 一个是源,一个目标。我们拖动源到目标。

代码语言:javascript
复制
<Page
    x:Class="Textvt.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:Textvt"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <StackPanel Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" Padding="30">

            <Border BorderBrush="Azure" BorderThickness="2">
                <TextBlock x:Name="TextSource" 
                           Text="我是源" 
                           CanDrag="True" 
                           DragStarting="Txtsource_OnDragStarting"  />
            </Border>
            <Border Margin="20" BorderBrush="Azure" BorderThickness="2"
                    AllowDrop="True" >
                <TextBlock x:Name="TextTarget" Text="目标TextBlock" 
                           Drop="Txttarget_OnDrop"
                           Height="50" Width="400"  
                           AllowDrop="True" 
                           DragEnter="Txttarget_OnDragEnter"/>
            </Border>
        </StackPanel>
    </Grid>
</Page>

在xaml.cs 需要3个事件,开始拖放,拖放,拖放进入。

其中拖放进入是设置鼠标显示的字和其他的东西,可以不要这个函数,不会影响功能。

代码语言:javascript
复制
        private void Txtsource_OnDragStarting(UIElement sender, DragStartingEventArgs args)
        {
            //开始拖放
            //设置拖放文字,文字是我们点击的TextBlock
            args.Data.SetText(TextSource.Text);
        }

        private async void Txttarget_OnDrop(object sender, DragEventArgs e)
        {
            //如果有文字,那么就是把他放在要拖放的TextBlock
            bool hasText = e.DataView.Contains(StandardDataFormats.Text);
            //显示复制还是没有
            //拖动显示可以去我之前写的 [http://blog.csdn.net/lindexi_gd/article/details/49757187?locationNum=2&fps=1](http://blog.csdn.net/lindexi_gd/article/details/49757187?locationNum=2&fps=1 )
            e.AcceptedOperation = hasText ? DataPackageOperation.Copy : DataPackageOperation.None;
            if (hasText)
            {
                var text = await e.DataView.GetTextAsync();
                TextTarget.Text += "\n" + text;
            }
        }

        private void Txttarget_OnDragEnter(object sender, DragEventArgs e)
        {
            
            bool hasText = e.DataView.Contains(StandardDataFormats.Text);
            e.AcceptedOperation = hasText ? DataPackageOperation.Copy : DataPackageOperation.None;
            if (hasText)
            {
                if (e.DragUIOverride != null)
                {
                    e.DragUIOverride.Caption = "Drop here to insert text";
                }
            }
        }

代码:http://download.csdn.net/detail/lindexi_gd/9739764 可以的话给点积分。

问题原文:

https://stackoverflow.com/questions/41662650/how-to-move-text-from-one-textblock-to-another-textblock-using-drag-and-drop-in


本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档