WPF 拖放到 DataGrid [英] WPF drag and drop to DataGrid
问题描述
我一直在互联网上下搜索,但找不到一些可以帮助我的例子.
I have been searching the internet high and low but can't find some examples that can help me.
我正在 wpf 中开发一个应用程序,其中我使用了 DataGrid
;每个单元格都有一个 DataTemplate
和图像,马赛克样式;在网格的一侧,我有一些瓷砖可以在网格上使用.
I'm developing an application in wpf, in it I use a DataGrid
; each cell has a DataTemplate
with and image, in a mosaic style; on the side of the grid I have some tiles to use on the grid.
我可以拖动图块,但不能将它们放到网格上,因为我找不到放置到的单元格.有没有办法从拖动事件中获取单元格位置?
I'm able to drag the tiles but can't drop them on the grid because I can't find the cell to which make the drop. Is there a way to get a cell position from the the drag events?
谢谢
推荐答案
我假设您订阅了 DataGrid 上的放置事件.
I am assuming you suscribed to the drop event on the DataGrid.
由于每个单元格都有一个 DataTemplate,因此您可以从单元格订阅放置事件,并且您应该能够通过 e.Source 进行所需的修改.
Since you have a DataTemplate for each cell, you could subscribe to the drop event from the cell and you should be able to do the modification you want via e.Source.
如果需要,您可以为每列定义不同的 DataTemplate,以便您可以在列中进行计算,然后通过
If you need to you could define a different DataTemplate for each column so that you can figure at the column and then modifiy the data via
BusinessObject row = ((UIElementResponsibleForTeDrop)e.Source).DataContext;
这篇关于WPF 拖放到 DataGrid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!