潜移默化学会WPF--拖放 学习(一) - AYUI框架 - 博客园

原文: 潜移默化学会WPF--拖放 学习(一) - AYUI框架 - 博客园

复制代码
 /// <summary>
    /// Window1.xaml 的交互逻辑
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();

            this.rectangle1.PreviewMouseMove += new MouseEventHandler(rectangle1_PreviewMouseMove);

            this.canvas2.DragOver += new DragEventHandler(canvas2_DragOver);
            this.canvas2.Drop += new DragEventHandler(canvas2_Drop);
        }

        void canvas2_Drop(object sender, DragEventArgs e)
        {
            IDataObject data = e.Data;

            if (data.GetDataPresent(typeof(Rectangle)))
            {
                Rectangle rect = data.GetData(typeof(Rectangle)) as Rectangle;
                this.canvas1.Children.Remove(rect);
                this.canvas2.Children.Add(rect);
            }
        }

        void canvas2_DragOver(object sender, DragEventArgs e)
        {
            if (!e.Data.GetDataPresent(typeof(Rectangle)))
            {
                e.Effects = DragDropEffects.None;
                e.Handled = true;
            }
        }

        void rectangle1_PreviewMouseMove(object sender, MouseEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                DataObject data = new DataObject(typeof(Rectangle), this.rectangle1);
                DragDrop.DoDragDrop(this.rectangle1, data, DragDropEffects.Move);
            }
        }
复制代码

 

 

前台

复制代码
<Window x:Class="DragDropDemo1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="319" Width="494">
    <Grid>
        <Canvas Margin="14,19,216,20" Name="canvas1" Background="Azure">
            <Rectangle Height="53" Name="rectangle1" Stroke="Black" Width="91" Fill="Crimson" />
        </Canvas>
        <Canvas HorizontalAlignment="Right" Margin="0,22,5,21" Name="canvas2" Width="199" Background="DarkSalmon" AllowDrop="True" />
    </Grid>
</Window>
复制代码

 

 

本例子来于网上 http://msdn.microsoft.com/zh-cn/ff685657.aspx  本文来自.NET开发者 作者:苏扬

分类: WPF
0
0
« 上一篇: 潜移默化学会WPF(技巧篇)--关于后台如何设置字符串的颜色的方法
» 下一篇: 时间查询
posted on 2019-03-01 22:42 NET未来之路 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/lonelyxmas/p/10459194.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值