Gong-wpf-dragdrop

来自软件开发
跳转至: 导航搜索

一个列表内选项的拖动,和不同列表间内容拖动的组件。不同列表间的拖动需要数据类型必须一致。

安装:

Install-Package gong-wpf-dragdrop

引用:

xmlns:dd="clr-namespace:GongSolutions.Wpf.DragDrop;assembly=GongSolutions.Wpf.DragDrop"

前台界面:

<Window x:Class="MoonSnake.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:MoonSnake"
        xmlns:dd="clr-namespace:GongSolutions.Wpf.DragDrop;assembly=GongSolutions.Wpf.DragDrop"
        mc:Ignorable="d"
        Title="Moon Snake Game" Height="540" Width="800" WindowStartupLocation="CenterScreen">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <DataGrid x:Name="grid1" dd:DragDrop.IsDragSource="True" dd:DragDrop.IsDropTarget="True"/>
        <DataGrid x:Name="grid2"  dd:DragDrop.IsDragSource="True" dd:DragDrop.IsDropTarget="True" Grid.Column="1"/>
    </Grid>
</Window>

后台界面:

using System.Collections.ObjectModel;
using System.Windows;

namespace MoonSnake
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            {
                ObservableCollection<TestClass> testClasses = new ObservableCollection<TestClass>();
                testClasses.Add(new TestClass() { Name = "a", Age = 20 });
                testClasses.Add(new TestClass() { Name = "b", Age = 21 });
                testClasses.Add(new TestClass() { Name = "c", Age = 22 });
                testClasses.Add(new TestClass() { Name = "d", Age = 23 });
                grid1.ItemsSource = testClasses;
            }

            {
                ObservableCollection<TestClass> testClasses = new ObservableCollection<TestClass>();
                testClasses.Add(new TestClass() { Name = "e", Age = 20 });
                testClasses.Add(new TestClass() { Name = "f", Age = 21 });
                testClasses.Add(new TestClass() { Name = "g", Age = 22 });
                testClasses.Add(new TestClass() { Name = "h", Age = 23 });
                grid2.ItemsSource = testClasses;
            }
        }
    }

    public class TestClass
    {
        public string Name { get; set; }
        public int Age { get; set; }
    }
}