WPF DataGrid 行拖拽实现

本文介绍了如何实现WPF DataGrid的行拖拽功能。通过自定义DataGrid控件,添加Popup并利用AutoGeneratingColumn事件生成模板。同时,利用Drop和MouseLeftButtonDown事件实现拖放操作。需注意将DataGrid的AllowDrop属性设为True,并使用Blend辅助编辑样式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

        DataGrid行拖拽需要我们自己去实现,我也是模仿网上例子进行改造。

        大概思路,我们需要自定义一个DataGrid的控件,然后再样式中加入一个Popup(作为被拖拽行的显示),我们要弄成所有都可以使用,就需要将Popup内容显示的模板实现自动生成,我们可以在AutoGeneratingColumn事件里实现,我是使用XMAL的方式保存所有的控件,然后赋值给我们自定义的一个DataTemplate属性。

        拖拽主要用到Drop、MouseLeftButtonDown事件。(需要将DataGird的AllowDrop设置为True)。

        伪代码(自己自行去完整):

<Popup AllowsTransparency = "True" IsOpen = "{Binding IsPopup, RelativeSource = "{RelativeSource  Mode = FindAncesTor, AncestorType = {x:Type xxxxxx}}}" 
Foucusable = "False" IsHitestVisible = "False" Placement = "RelativePoint"
PlacementRectangle = "{Binding PopupPlacementRectangle, RelativeSource = "{RelativeSource  Mode = FindAncesTor, AncestorType = {x:Type xxxxxx}}}" >
    <Grid>
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值