WPF中拖动数据到另一个控件中

先说一下流程

1.选择要拖动的数据

2.拖动

3.放入拖动的目标控件中。

开始。

1.先看界面,我们要把界面中的 Grid1 Grid2 Grid3 和Label的内容,分别拖动到TextBox中。

2.

在Grid中,增加MouseDown="Grid_MouseDown"事件,

在TextBox中增加DragDrop.Drop="TextBox_Drop"事件,

在Label中增加Label_MouseDown事件

注意:拖动的地方增加MouseDown事件,放入的地方增加DragDrop.Drop,一定要用DragDrop.Drop,不能使用别的,不然鼠标松下的那一刻,数据不会放下。

AllowDrop="True" 有些控件需要增加这个属性

3.效果

<Window x:Class="WpfApp1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp1"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <Grid Name="grid11" HorizontalAlignment="Left" Height="100" Margin="136,22,0,0" Grid.Row="1" VerticalAlignment="Top" Width="100" MouseDown="Grid_MouseDown">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="0*" />
                <ColumnDefinition/>
                <ColumnDefinition />
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="*" />
                <RowDefinition Height="*" />
            </Grid.RowDefinitions>
            <TextBlock Grid.ColumnSpan="2">Grid 1</TextBlock>
            <TextBlock Grid.Column="2">Grid 2</TextBlock>
            <TextBlock Grid.Row="1" Grid.ColumnSpan="3">Grid 3</TextBlock>
        </Grid>
        <TextBox Name="txtA" HorizontalAlignment="Left" Height="70" Margin="483,205,0,0" TextWrapping="Wrap" DragDrop.Drop="TextBox_Drop"  Text="TextBox" VerticalAlignment="Top" Width="185"/>
        <Label  Name="lblA" Content="Label" AllowDrop="True"  HorizontalAlignment="Left" Margin="192,257,0,0" VerticalAlignment="Top" MouseDown="Label_MouseDown"/>
    </Grid>
</Window>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Xml;

namespace WpfApp1
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Grid_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                DragDrop.DoDragDrop(grid11, ((System.Windows.Controls.TextBlock)e.Source).Text, DragDropEffects.Copy);
            }
        }

        private void TextBox_Drop(object sender, DragEventArgs e)
        {
            txtA.Text = e.Data.GetData(DataFormats.Text).ToString();
            e.Effects = DragDropEffects.Copy;
        }

        private void Label_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                DragDrop.DoDragDrop(lblA, ((System.Windows.Controls.Label)e.Source).Content, DragDropEffects.Copy);
            }
        }
    }
}

来源:WPF中拖动数据到另一个控件中-CSDN博客

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

故里2130

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值