WPF 精修篇 拖拽 DragDrop

WPF 实现拖拽

效果

   <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="197*"/>
            <ColumnDefinition Width="209*"/>
            <ColumnDefinition Width="111*"/>
        </Grid.ColumnDefinitions>
        <WrapPanel x:Name="accept" HorizontalAlignment="Left" Height="320" VerticalAlignment="Top" Width="207" Grid.Column="1" Margin="2,0,0,0" Background="#FFE7FFE5" Drop="WrapPanel_Drop" AllowDrop="True" />
        <WrapPanel x:Name="send" HorizontalAlignment="Left" Height="320" VerticalAlignment="Top" Width="197" Background="#FFFFEDCD" MouseLeftButtonDown="WrapPanel_MouseLeftButtonDown">
            <Label Content="Label1" Height="31" Width="51" Background="#FFDCA1A1" Margin="5"/>
            <Label Content="Label2" Height="31" Width="51" Background="#FFDCA1A1" Margin="5"/>
            <Label Content="Label3" Height="31" Width="51" Background="#FFDCA1A1" Margin="5"/>
            <Label Content="Label4" Height="31" Width="51" Background="#FFDCA1A1" Margin="5"/>
            <Label Content="Label5" Height="31" Width="51" Background="#FFDCA1A1" Margin="5"/>
            <Label Content="Label6" Height="31" Width="51" Background="#FFDCA1A1" Margin="5"/>
        </WrapPanel>

    </Grid>
     private void WrapPanel_Drop(object sender, DragEventArgs e)
        {
      
            var item = e.Data;
            object data = item.GetData(item.GetFormats()[0]);
            if (data is UIElement) 
            {
                send.Children.Remove(data as UIElement);
                accept.Children.Add(data as UIElement);
            }
        
        }

        private void WrapPanel_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            object item = e.Source;
            if (item is UIElement) 
            {
                DragDrop.DoDragDrop(item as UIElement, item, DragDropEffects.Move);
            }
        }

accept 一方的控件 需要加上  AllowDrop="True" 允许接收drop的数据

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
以下是一个简单的 WPF DragDrop 例子: XAML 代码: ```xml <Window x:Class="WpfApp1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="450" Width="800"> <Grid> <StackPanel Orientation="Horizontal" Margin="10"> <TextBlock Text="Drag me:" Margin="0,0,10,0" /> <TextBlock Text="Hello, world!" Background="LightGray" Width="100" Height="50" PreviewMouseLeftButtonDown="TextBlock_PreviewMouseLeftButtonDown" PreviewMouseMove="TextBlock_PreviewMouseMove" PreviewMouseLeftButtonUp="TextBlock_PreviewMouseLeftButtonUp" /> </StackPanel> <StackPanel Orientation="Horizontal" Margin="10"> <TextBlock Text="Drop here:" Margin="0,0,10,0" /> <Border BorderBrush="Black" BorderThickness="1" Width="100" Height="50" AllowDrop="True" Drop="Border_Drop" /> </StackPanel> </Grid> </Window> ``` C# 代码: ```csharp using System.Windows; using System.Windows.Controls; using System.Windows.Input; using System.Windows.Media; namespace WpfApp1 { public partial class MainWindow : Window { private bool isDragging = false; private Point startPoint; public MainWindow() { InitializeComponent(); } private void TextBlock_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e) { startPoint = e.GetPosition(null); isDragging = true; } private void TextBlock_PreviewMouseMove(object sender, MouseEventArgs e) { if (isDragging && e.LeftButton == MouseButtonState.Pressed) { Point position = e.GetPosition(null); Vector offset = startPoint - position; if (Math.Abs(offset.X) > SystemParameters.MinimumHorizontalDragDistance || Math.Abs(offset.Y) > SystemParameters.MinimumVerticalDragDistance) { DataObject data = new DataObject("myFormat", "Hello, world!"); DragDrop.DoDragDrop((DependencyObject)sender, data, DragDropEffects.Copy); isDragging = false; } } } private void TextBlock_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e) { isDragging = false; } private void Border_Drop(object sender, DragEventArgs e) { if (e.Data.GetDataPresent("myFormat")) { string text = (string)e.Data.GetData("myFormat"); ((Border)sender).Child = new TextBlock { Text = text, Background = Brushes.LightGray }; } } } } ``` 这个例子演示了如何在 WPF 中实现拖放操作。我们创建了一个包含两个 StackPanel 的 Window,第一个 StackPanel 包含一个 TextBlock,可以被拖动,第二个 StackPanel 包含一个 Border,可以接受拖放操作。当用户按下鼠标左键并移动 TextBlock 时,如果移动距离超过一定阈值,就会触发拖放操作。在拖放操作中,我们将一个字符串放入 DataObject 中,并指定数据格式为 "myFormat"。在 Border 的 Drop 事件中,我们检查数据格式是否为 "myFormat",如果是,就将字符串显示在 Border 中。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

小慧哥

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

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

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

打赏作者

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

抵扣说明:

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

余额充值