Gong WPF DragDrop 项目教程

Gong WPF DragDrop 项目教程

gong-wpf-dragdropThe GongSolutions.WPF.DragDrop library is a drag'n'drop framework for WPF项目地址:https://gitcode.com/gh_mirrors/go/gong-wpf-dragdrop

1. 项目的目录结构及介绍

Gong WPF DragDrop 项目的目录结构如下:

gong-wpf-dragdrop/
├── GongSolutions.Wpf.DragDrop/
│   ├── Behaviours/
│   ├── DefaultDropHandler.cs
│   ├── DefaultDragHandler.cs
│   ├── DropInfo.cs
│   ├── DragDrop.cs
│   ├── DragDrop.Behaviours.cs
│   ├── DragDrop.csproj
│   └── ...
├── GongSolutions.Wpf.DragDrop.Tests/
│   ├── DragDropTests.cs
│   ├── DragDropTests.csproj
│   └── ...
├── GongSolutions.Wpf.DragDrop.Demo/
│   ├── MainWindow.xaml
│   ├── MainWindow.xaml.cs
│   ├── App.xaml
│   ├── App.xaml.cs
│   ├── GongSolutions.Wpf.DragDrop.Demo.csproj
│   └── ...
├── .gitignore
├── LICENSE
├── README.md
└── ...

目录结构介绍

  • GongSolutions.Wpf.DragDrop: 包含拖放功能的核心实现代码。

    • Behaviours: 包含拖放行为的相关类。
    • DefaultDropHandler.cs: 默认的放置处理程序。
    • DefaultDragHandler.cs: 默认的拖动处理程序。
    • DropInfo.cs: 放置信息类。
    • DragDrop.cs: 拖放功能的主类。
    • DragDrop.Behaviours.cs: 拖放行为的扩展类。
    • DragDrop.csproj: 项目文件。
  • GongSolutions.Wpf.DragDrop.Tests: 包含拖放功能的单元测试。

    • DragDropTests.cs: 拖放功能的测试类。
    • DragDropTests.csproj: 测试项目文件。
  • GongSolutions.Wpf.DragDrop.Demo: 包含拖放功能的演示应用程序。

    • MainWindow.xaml: 主窗口的 XAML 文件。
    • MainWindow.xaml.cs: 主窗口的后台代码文件。
    • App.xaml: 应用程序的 XAML 文件。
    • App.xaml.cs: 应用程序的后台代码文件。
    • GongSolutions.Wpf.DragDrop.Demo.csproj: 演示项目文件。
  • .gitignore: Git 忽略文件。

  • LICENSE: 项目许可证文件。

  • README.md: 项目说明文件。

2. 项目的启动文件介绍

Gong WPF DragDrop 项目的启动文件位于 GongSolutions.Wpf.DragDrop.Demo 目录下:

  • App.xaml: 应用程序的 XAML 文件,定义了应用程序的资源和其他全局设置。
  • App.xaml.cs: 应用程序的后台代码文件,包含应用程序的启动逻辑。

App.xaml

<Application x:Class="GongSolutions.Wpf.DragDrop.Demo.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <!-- 应用程序资源定义 -->
    </Application.Resources>
</Application>

App.xaml.cs

namespace GongSolutions.Wpf.DragDrop.Demo
{
    public partial class App : Application
    {
        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);
            // 应用程序启动逻辑
        }
    }
}

3. 项目的配置文件介绍

Gong WPF DragDrop 项目没有特定的配置文件,因为它主要是一个库项目,依赖于应用程序项目(如 GongSolutions.Wpf.DragDrop.Demo)来提供配置。

依赖项配置

GongSolutions.Wpf.DragDrop.Demo 项目的 GongSolutions.Wpf.DragDrop.Demo.csproj 文件中,可以找到项目的依赖项配置:

<Project Sdk="Microsoft.NET.

gong-wpf-dragdropThe GongSolutions.WPF.DragDrop library is a drag'n'drop framework for WPF项目地址:https://gitcode.com/gh_mirrors/go/gong-wpf-dragdrop

  • 10
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
以下是一个简单的 WPF DragDrop 例子: XAML 代码: ```xml <Window x:Class="WpfApp1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="450" Width="800"> <Grid> <StackPanel Orientation="Horizontal" Margin="10"> <TextBlock Text="Drag me:" Margin="0,0,10,0" /> <TextBlock Text="Hello, world!" Background="LightGray" Width="100" Height="50" PreviewMouseLeftButtonDown="TextBlock_PreviewMouseLeftButtonDown" PreviewMouseMove="TextBlock_PreviewMouseMove" PreviewMouseLeftButtonUp="TextBlock_PreviewMouseLeftButtonUp" /> </StackPanel> <StackPanel Orientation="Horizontal" Margin="10"> <TextBlock Text="Drop here:" Margin="0,0,10,0" /> <Border BorderBrush="Black" BorderThickness="1" Width="100" Height="50" AllowDrop="True" Drop="Border_Drop" /> </StackPanel> </Grid> </Window> ``` C# 代码: ```csharp using System.Windows; using System.Windows.Controls; using System.Windows.Input; using System.Windows.Media; namespace WpfApp1 { public partial class MainWindow : Window { private bool isDragging = false; private Point startPoint; public MainWindow() { InitializeComponent(); } private void TextBlock_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e) { startPoint = e.GetPosition(null); isDragging = true; } private void TextBlock_PreviewMouseMove(object sender, MouseEventArgs e) { if (isDragging && e.LeftButton == MouseButtonState.Pressed) { Point position = e.GetPosition(null); Vector offset = startPoint - position; if (Math.Abs(offset.X) > SystemParameters.MinimumHorizontalDragDistance || Math.Abs(offset.Y) > SystemParameters.MinimumVerticalDragDistance) { DataObject data = new DataObject("myFormat", "Hello, world!"); DragDrop.DoDragDrop((DependencyObject)sender, data, DragDropEffects.Copy); isDragging = false; } } } private void TextBlock_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e) { isDragging = false; } private void Border_Drop(object sender, DragEventArgs e) { if (e.Data.GetDataPresent("myFormat")) { string text = (string)e.Data.GetData("myFormat"); ((Border)sender).Child = new TextBlock { Text = text, Background = Brushes.LightGray }; } } } } ``` 这个例子演示了如何在 WPF 中实现拖放操作。我们创建了一个包含两个 StackPanel 的 Window,第一个 StackPanel 包含一个 TextBlock,可以被拖动,第二个 StackPanel 包含一个 Border,可以接受拖放操作。当用户按下鼠标左键并移动 TextBlock 时,如果移动距离超过一定阈值,就会触发拖放操作。在拖放操作中,我们将一个字符串放入 DataObject 中,并指定数据格式为 "myFormat"。在 Border 的 Drop 事件中,我们检查数据格式是否为 "myFormat",如果是,就将字符串显示在 Border 中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

柏纲墩Dean

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值