WPF 拖放到 DataGrid

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2534040/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 21:37:47  来源:igfitidea点击:

WPF drag and drop to DataGrid

wpfdatagriddrag-and-drop

提问by user267752

I have been searching the internet high and low but can't find some examples that can help me.

我一直在互联网上下搜索,但找不到一些可以帮助我的例子。

I'm developing an application in wpf, in it I use a DataGrid; each cell has a DataTemplatewith and image, in a mosaic style; on the side of the grid I have some tiles to use on the grid.

我正在 wpf 中开发一个应用程序,其中我使用了一个DataGrid; 每个单元格都有一个DataTemplate带有和图像,以马赛克风格;在网格的一侧,我有一些瓷砖可以在网格上使用。

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?

我可以拖动图块但不能将它们放在网格上,因为我找不到放置的单元格。有没有办法从拖动事件中获取单元格位置?

Thanks

谢谢

回答by Ostas

I am assuming you suscribed to the drop event on the DataGrid.

我假设您订阅了 DataGrid 上的 drop 事件。

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,因此您可以从单元格订阅 drop 事件,并且您应该能够通过 e.Source 进行所需的修改。

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

如果您需要,您可以为每列定义一个不同的 DataTemplate,以便您可以在列中进行计算,然后通过修改数据

BusinessObject row = ((UIElementResponsibleForTeDrop)e.Source).DataContext;