首页 > Windows开发 > 详细

WPF Item拖拽 DragDrop

时间:2019-03-04 15:41:46      阅读:233      评论:0      收藏:0      [点我收藏+]

今天有个需求是需要拖拽DataGrid中的item到另一个DataGrid。自己实现还比较繁琐,网上查了查,发现一个不错的开源项目

gong-wpf-dragdrop

nuget安装下:Install-Package gong-wpf-dragdrop -Version 1.1.0

在xaml中添加引用: xmlns:dd="clr-namespace:GongSolutions.Wpf.DragDrop;assembly=GongSolutions.Wpf.DragDrop"

然后在datagrid中添加附加属性

<Window x:Class="WpfApp8.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:WpfApp8"
        xmlns:dd="clr-namespace:GongSolutions.Wpf.DragDrop;assembly=GongSolutions.Wpf.DragDrop"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <DataGrid x:Name="grid1" dd:DragDrop.IsDragSource="True" dd:DragDrop.IsDropTarget="True" HorizontalAlignment="Left" Height="344" Margin="42,37,0,0" VerticalAlignment="Top" Width="292"/>


        <DataGrid x:Name="grid2"  dd:DragDrop.IsDragSource="True" dd:DragDrop.IsDropTarget="True"  HorizontalAlignment="Left" Height="344" Margin="415,37,0,0" VerticalAlignment="Top" Width="321"/>
        
    </Grid>
</Window>

dd:DragDrop.IsDragSource="True" dd:DragDrop.IsDropTarget="True"

后台绑定下数据源

技术分享图片

注:如果grid2的数据源item类型与grid1不一致,是不可以拖拽的。

技术分享图片

可以支持多行拖拽。

WPF Item拖拽 DragDrop

原文:https://www.cnblogs.com/czly/p/10470759.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!