删除 WPF DataGrid 中选定的单元格内容
我有一个绑定到矩阵的 DataGrid。 该应用程序的主要逻辑是任何值为 0 的单元格都不会显示任何内容(0 = 我的引用中没有值)。
现在我想要的是,当用户按下删除按钮时,所有选定的单元格将被设置为 0。 我可以轻松地用一个单元格来做到这一点(因为我跟踪当前选定的行/列),但我无法正确获得 2x2 的正方形。
我想出的最好方法是使用 Binding
魔法来获取所选每个单元格的行/列索引,然后将 Binding
的源设置为 0。但是,我仍然无法获得所有行号(基本上是因为 grid.SelectedCells
是一个 IEnumerable
,而且我只能使用此获取列对象,我无法获取当前行。)
当我仅选择一个单元格(或一行)时,它可以使用以下命令:
DataGridRow dataGridRow = (DataGridRow)this.ItemContainerGenerator.ContainerFromItem(grid.CurrentItem);
多重选择怎么样?
这里有什么想法吗?谢谢!
I have a DataGrid
bound to a matrix.
The main logic of the application is that any cell having a value of 0 will just display nothing (0 = no value in my referential).
Now what I want is that, when the user presses the Delete button, all the cells selected will be set to 0.
I can easily do that with one cell (because I keep track of the current row/column selected), but I can't get, let's say, a 2x2 square correctly.
The best way I figured out, using Binding
magic, would be to get the row/column indexes of each cell selected, and then set the Binding
's source to 0. But still, I can't have all of the row numbers (basically because grid.SelectedCells
is an IEnumerable<DataGridCellInfo>
, and I can only get the column with this object, I have no way to get the current row.)
It works when I only select one cell(or one row), by using the following:
DataGridRow dataGridRow = (DataGridRow)this.ItemContainerGenerator.ContainerFromItem(grid.CurrentItem);
How about multiple selection?
Any ideas here? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试以下方法获取选定单元格的值:
计算单元格是文本块。
Try this to get to the selected cells value:
Counting that the cells are textblocks.