首页 > Windows开发 > 详细

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

时间:2019-03-01 23:15:16      阅读:155      评论:0      收藏:0      [点我收藏+]
原文:潜移默化学会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--拖放 学习(一) - AYUI框架 - 博客园

原文:https://www.cnblogs.com/lonelyxmas/p/10459194.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!