#711 – 在拖拽的过程中改变鼠标样式(Changing the Mouse Cursor While Dragging)

来源:互联网 发布:js导航代码 编辑:程序博客网 时间:2024/05/18 22:42

原文地址:https://wpf.2000things.com/2012/12/13/711-changing-the-mouse-cursor-while-dragging/

在WPF拖拽的过程中,通过GiveFeedback 事件可以更改整个过程中鼠标的样式。在事件中,我们可以通过GiveFeedbackEventArgs.Effects 属性来判断当前位置的拖动效果,从而设置鼠标的样式。

下面的例子中,当拖动的效果为Copy的时候,我们改变鼠标为一个手的样式,来表示允许拖动数据。

<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;}

上面的最后一个函数中如果Effects 属性为Copy ,我们将鼠标设置为Cursors.Hand ,其它情况下为默认样式。

711-001
711-002

******************************************译者注*****************************************

GiveFeedbackEventArgs.Effects 属性是一个DragDropEffects类型的枚举,它有以下几种枚举值:

  None - 放置目标不接受该数据。

  Copy - 将拖动源中的数据复制到放置目标。

  Move - 将拖动源的数据移动到放置目标。

 Link - 将拖动源中的数据链接到放置目标。

 Scroll - 拖动时可以滚动目标,以定位在目标中当前不可见的某个放置位置。

 All - Copy、Link、Move 和 Scroll 效果的组合。


0 0
原创粉丝点击