• WPF中的拖放1


    实现了WPF的不同层级间的元素之间的拖放,例子虽小却很经典,引申一下也许可以实现类VS界面的浮动依靠面板。

    拖放前:

    拖放后:

    代码如下:

    <Window x:Class="WpfApplication8.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="350" Width="525">
        <Grid>
            <Canvas Margin="0,0,216,0" Name="canvas1" Background="Crimson" AllowDrop="True" Drop="C1Drop">
                <Rectangle Height="100" Margin="10,10" Name="rectangle1"  Width="100" Fill="Azure" PreviewMouseMove="RectMove" />
            </Canvas>
            <Canvas HorizontalAlignment="Right" Margin="0,0,0,0" Name="canvas2" 
                    Width="199" Background="DarkSalmon" AllowDrop="True" Drop="C2Drop"/>
        </Grid>
    </Window>
            private void C1Drop(object sender, DragEventArgs e)
            {
                if (e.Data.GetDataPresent(typeof(Rectangle)))
                {
                    string str = ((Canvas)(this.rectangle1.Parent)).Name.ToString();                
                    if(str.Equals("canvas2"))
                    {
                        Rectangle rect = (Rectangle)e.Data.GetData(typeof(Rectangle));
                        this.canvas2.Children.Remove(rect);
                        this.canvas1.Children.Add(rect);
                    }
                  
                }
            }
            private void C2Drop(object sender, DragEventArgs e)
            {
                if (e.Data.GetDataPresent(typeof(Rectangle)))
                {
                    string str = ((Canvas)(this.rectangle1.Parent)).Name.ToString();
                    if (str.Equals("canvas1"))
                    {
                        Rectangle rect = (Rectangle)e.Data.GetData(typeof(Rectangle));
                        this.canvas1.Children.Remove(rect);
                        this.canvas2.Children.Add(rect);
                    }
                }
            }
            private void RectMove(object sender, MouseEventArgs e)
            {
                if (e.LeftButton == MouseButtonState.Pressed)       // 如果鼠标左键被按下
                {
                    DataObject data = new DataObject(typeof(Rectangle), this.rectangle1);
                    DragDrop.DoDragDrop(this.rectangle1, data, DragDropEffects.Move);
                }
            }

    感觉博主苏扬的分享:http://msdn.microsoft.com/zh-cn/ff685657.aspx

  • 相关阅读:
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    Scapy 工具介绍
    ubuntu虚拟机使用open-vm-tools代替vmware-tools
    docker、vmware和PD的区别
    ubuntu查看OpenGL版本
    SQL Server 常用近百条SQL语句(收藏版)
    Intellij IDEA 如何去掉 @Autowired 注入警告
    awtk-linux-fb 使用 double framebuffer 闪烁的问题
  • 原文地址:https://www.cnblogs.com/Roarsun/p/3470404.html
Copyright © 2020-2023  润新知