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

原文地址: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 效果的组合。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值