#730 – 使用QueryContinueDrag 事件(Use QueryContinueDrag Event to Know When Mouse Button State Changes)

本文介绍如何使用WPF中的QueryContinueDrag事件来检测拖拽过程中鼠标按键状态的变化,并通过示例展示了当鼠标左键释放时如何更新源控件的内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原文地址:https://wpf.2000things.com/2013/01/09/730-use-querycontinuedrag-event-to-know-when-mouse-button-state-changes/

当在拖拽的操作过程中,鼠标或者键盘的按键状态(Ctrl, Shift 或者Alt)发生改变,QueryContinueDrag 事件就会被触发。

下面的例子中,当拖拽的过程中鼠标左键被放开时,改变源控件中的内容。

<Label Content="Drag from here" Background="LavenderBlush"
       HorizontalAlignment="Center" Margin="10" Padding="10"
       MouseDown="Label1_MouseDown"
       QueryContinueDrag="Label1_QueryContinueDrag"/>
<Label Content="To here" Background="SandyBrown" AllowDrop="True"
       HorizontalAlignment="Center" Margin="10" Padding="10"
       Drop="Label2_Drop"/>
上面两个Label,第一个作为拖动的源,第二个是接收拖动的控件。下面是CS代码。

private void Label1_MouseDown(object sender, MouseButtonEventArgs e)
{
    Label lblFrom = e.Source as Label;
 
    if (e.LeftButton == MouseButtonState.Pressed)
        DragDrop.DoDragDrop(lblFrom, lblFrom.Content, DragDropEffects.Copy);
}
 
private void Label1_QueryContinueDrag(object sender, QueryContinueDragEventArgs e)
{
    Label lblFrom = e.Source as Label;
 
    if (!e.KeyStates.HasFlag(DragDropKeyStates.LeftMouseButton))
        lblFrom.Content = "...";
}
 
private void Label2_Drop(object sender, DragEventArgs e)
{
    string draggedText = (string)e.Data.GetData(DataFormats.StringFormat);
 
    Label toLabel = e.Source as Label;
    toLabel.Content = draggedText;
}
730-001
730-002

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值