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

来源:互联网 发布:任意号码显示软件 编辑:程序博客网 时间:2024/05/17 21:49

原文地址: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

阅读全文
0 0