65.9K
CodeProject 正在变化。 阅读更多。
Home

DragSource - 一个 WPF 拖放助手

starIconstarIconstarIconstarIconstarIcon

5.00/5 (4投票s)

2013年7月17日

Ms-PL

2分钟阅读

viewsIcon

16256

downloadIcon

754

介绍 Nicenis 项目中的 DragSource 类。

引言

DragSource 是一个静态类,用于帮助创建可拖动的元素,用于拖放操作。该类是 Nicenis 项目的一部分。您可以在 CodePlex 项目主页 https://nicenis.codeplex.com 上找到最新信息。

要求

您需要引用 Nicenis.dll。还需要 .NET Framework 4 客户端配置文件或更高版本。

基本用法

至少需要设置两个附加属性

  • AllowDrag:指示元素是否可拖动。
  • Data:一个数据对象,包含正在拖动的数据。

这是一个例子。

<Window
    x:Class="DragSourceSample.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:n="clr-namespace:Nicenis.Windows;assembly=Nicenis"
    Title="MainWindow" Height="200" Width="200">

    <!-- This border will be draggable.-->
    <Border
        n:DragSource.AllowDrag="True"
        n:DragSource.Data="Test Data"
        Margin="30"
        Background="Green"
        />
</Window>

拖动边框时,它看起来会像这张图片。

默认情况下,拖动时不会保留在边框中单击的位置。您可以使用以下附加属性来解决它。

  • VisualFeedbackOffset:指向设备在视觉拖放反馈中的偏移量。
  • ContactPosition:拖动源中的只读接触位置。

这是一个改进的例子。

<Window
    x:Class="DragSourceSample.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:n="clr-namespace:Nicenis.Windows;assembly=Nicenis"
    Title="MainWindow" Height="200" Width="200">

    <!-- This border will be draggable.-->
    <Border
        n:DragSource.AllowDrag="True"
        n:DragSource.Data="Test Data"
        n:DragSource.VisualFeedbackOffset="{Binding (n:DragSource.ContactPosition), RelativeSource={RelativeSource Self}}"
        Margin="30"
        Background="Green"
        />
</Window>

延迟数据对象创建

您可以将 Data 附加属性设置为 IDataObjectProvider 接口的实现。

namespace Nicenis.Windows
{
    /// <summary>
    /// Provides a way to get a data object that contains the data being dragged.
    /// </summary>
    public interface IDataObjectProvider
    {
        /// <summary>
        /// Gets a data object that contains the data being dragged.
        /// </summary>
        /// <returns>A data object that contains the data being dragged.</returns>
        object GetDataObject();
    }
}	

GetDataObject 方法在开始拖放操作之前调用。一个示例实现如下所示。

/// <summary>
/// A sample data context that implements the IDataObjectProvider interface.
/// </summary>
public class SampleDataContext : IDataObjectProvider
{
    public object GetDataObject()
    {
        return "Test Data";
    }
}

以下代码显示了如何将其绑定到 Data 附加属性。

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        // Initializes the DataContext.
        DataContext = new SampleDataContext();
    }
}
<Window
    x:Class="DragSourceSample.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:n="clr-namespace:Nicenis.Windows;assembly=Nicenis"
    Title="MainWindow" Height="200" Width="200">

    <!-- The Data is bound to an implementation of the IDataObjectProvider interface.-->
    <Border
        n:DragSource.AllowDrag="True"
        n:DragSource.Data="{Binding}"
        n:DragSource.VisualFeedbackOffset="{Binding (n:DragSource.ContactPosition), RelativeSource={RelativeSource Self}}"
        Margin="30"
        Background="Green"
        />
</Window>

 

自定义视觉反馈

如果您需要自定义拖动图像(称为视觉反馈),可以使用 VisualFeedback 附加属性。

  • VisualFeedback:视觉拖放反馈的内容。

以下代码使用椭圆作为自定义视觉反馈。

<Window
    x:Class="DragSourceSample.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentationquot;
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:n="clr-namespace:Nicenis.Windows;assembly=Nicenis"
    Title="MainWindow" Height="200" Width="200"
    >
    <Border
        n:DragSource.AllowDrag="True"
        n:DragSource.Data="Test Data"
        n:DragSource.VisualFeedbackOffset="70 70"
        Margin="30"
        Background="Green"
        >
        <!-- An ellipsis is set as a visual feedback.-->
        <n:DragSource.VisualFeedback>
            <Ellipse Fill="Red" Width="140" Height="140" />
        </n:DragSource.VisualFeedback>
    </Border>
</Window>

拖动边框时,它看起来会像这样。

视觉反馈默认继承数据源的 DataContext。您可以使用以下附加属性来覆盖它。

  • VisualFeedbackDataContext:设置为视觉拖放反馈的数据上下文的对象。

如果您需要视觉反馈模板,以下附加属性可能会帮助您。

  • VisualFeedbackTemplate:视觉拖放反馈内容的 DataTemplate。
  • VisualFeedbackTemplateSelector:视觉拖放反馈内容的模板选择器。

 

触摸设备未就绪

该类尚未在触摸设备上进行测试。

© . All rights reserved.