WPF 拖拽

1、事件实现拖拽

前台代码:
<Label Content="事件实现拖拽" MouseLeftButtonDown="Label_MouseLeftButtonDown"/>

后台代码:
private void Label_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
this.DragMove();
}

2、Thumb 的三个事件 DragDelta、DragStarted、DragCompleted

https://www.cnblogs.com/zhoujg/archive/2009/11/23/1608913.html

private void DragDelta(object sender, System.Windows.Controls.Primitives.DragDeltaEventArgs e)
{
   Canvas.SetLeft(thumb1,Canvas.GetLeft(thumb1)+e.HorizontalChange);
   Canvas.SetTop(thumb1, Canvas.GetTop(thumb1) + e.VerticalChange);
}

private void DragStarted(object sender, System.Windows.Controls.Primitives.DragStartedEventArgs e)
{
   thumb1.Background = Brushes.White;
}

private void DragCompleted(object sender, System.Windows.Controls.Primitives.DragCompletedEventArgs e)
{
   thumb1.Background = Brushes.Red;
}

3、控件的 AllowDrop 属性与 Drop 事件

private void label1_MouseDown(object sender, MouseButtonEventArgs e)
{
    Label lbl = (Label)sender;
    DragDrop.DoDragDrop(lbl, lbl.Content, DragDropEffects.Copy);
}
private void tagert_drop(object sender, DragEventArgs e)
{
    ((Label)sender).Content = e.Data.GetData(DataFormats.Text);
}

步骤:

1、将要拖拽的数据通过 DragDrop.DoDrayDrop 方法写入到缓存中。

2、当将鼠标放在目标控件上,如果目标控件的 AllowDrop 属性打开,会触发 Drop 事件。

3、在 Drop 事件中,通过事件数据 DragEventArgs 取出,e.Data.Gate。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值