#711 – Changing the Mouse Cursor While Dragging
December 13, 2012 1 Comment
You can change the mouse cursor during a drag-and-drop operation by handling the GiveFeedback event for the drag source. The GiveFeedbackEventArgs.Effects property will indicate the current effect, depending on the mouse position. You can set the mouse cursor based on the effect.
In the example below, we change the mouse to a “hand” whenever the effect is Copy, indicating that we’re allowed to drop the data.
<StackPanel Orientation="Vertical" HorizontalAlignment="Center" Margin="45"> <Label Content="Data to drag" Background="AliceBlue" Padding="15,10" Margin="10" MouseLeftButtonDown="Label_MouseLeftButtonDown" GiveFeedback="Label_GiveFeedback"/> <Label Content="Drag to here" Background="MediumSpringGreen" Padding="15,10" Margin="10" AllowDrop="True" Drop="Label_Drop"/> </StackPanel>
private void Label_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { DataObject data = new DataObject(DataFormats.Text, ((Label)e.Source).Content); DragDrop.DoDragDrop((DependencyObject)e.Source, data, DragDropEffects.Copy); } private void Label_Drop(object sender, DragEventArgs e) { ((Label)e.Source).Content = (string)e.Data.GetData(DataFormats.Text); } private void Label_GiveFeedback(object sender, GiveFeedbackEventArgs e) { if (e.Effects == DragDropEffects.Copy) { e.UseDefaultCursors = false; Mouse.SetCursor(Cursors.Hand); } else e.UseDefaultCursors = true; e.Handled = true; }