如何在 WPF gridview 中查找选定单元格的像素坐标
这几天我一直在尝试回答这个问题。我发现了一些接近的代码片段,但没有给我我需要的东西。
我需要能够识别 WPF 数据网格中特定单元格的 x 和 y 坐标。该单元格将成为选定的单元格。我还需要能够确定该单元格的确切大小。
这样,当用户编辑该单元格时,我可以在该单元格上叠加编辑器控件,以便我可以接受用户输入。
必须这样做的原因是我需要按行使用不同的编辑器。数据网格中有几列。其中一列特别要求用户提供一个值。根据另一列的内容,用户需要在值列中提供的输入类型会发生变化。文本输入或数字输入需要文本框。有些行是布尔值,用户需要从复选框中进行选择。某些行具有预定义值,用户需要一个组合框。还有文件路径行,我希望自动出现一个文件打开对话框并将选定的文件名放入单元格中。所以我不能只使用数据网格中的内置编辑器,因为它允许我按列指定格式,但不能按行指定格式。当然,我不提前知道行的内容,因此需要在运行时完成。
我已经能够移动编辑器控件并使它们可见和不可见。 我已经能够获取所选单元格左侧的 X 坐标。 我还能够找到所选单元格的宽度。
单元格的 Y 坐标和单元格的高度 我尝试了几种不同的方法,但没有任何效果。
我已经弄清楚了如何做到这一点的逻辑流程。我在窗口窗体应用程序中做了一个示例运行。 100 行代码,大约 30 分钟,效果很好。几天来我一直在试图弄清楚如何在 WPF 中做同样的事情。任何帮助将不胜感激。
I have been trying for a few days to answer this question. I've found snippets of code which come close but have not given me what I need.
I need to be able to identify the x and y coordinates of a specific cell in a WPF datagrid. the cell will be the selected cell. I also need to be able to determine the exact size of that cell.
this is so that I can superimpose an editor control over that cell when the user edits it so I can accept user input.
the reason it has to be done this way is I need different editors by row. there are several columns in the datagrid. one column in particular asks the user to provide a value. depending on the contents of another column the type of input the user needs to provide in the value column changes. a text input or a number input requires a text box. some rows are boolean and the user needs to select from a check box. some rows have predefined values and the user needs a combo box. there are also file path rows and I want a file open dialog to come up automatically and place the selected filename into the cell. so I can't just use the built in editors in the datagrid because it allows me to specify the format by column, but not also by row. of course I don't know the contents of the rows ahead of time so it needs to be done at runtime.
I have been able to move the editor controls around and make them visible and invisible.
I have been able to get the X coordinate of the left side of the selected cell.
I've also been able to find the width of the selected cell.
the Y coordinate of the cell and the height of the cell I have attempted in several different ways but none have worked.
I already have the logic flow figured out for how to do this. I did a sample run in a window forms application. 100 lines of code and about 30 minutes and it was working perfectly. I've been trying to figure out how to do the same thing in WPF for days. any help would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一个不太优雅的解决方案:
只有 X 在那里,但 Y 是相同的。主要问题是需要一个开关来确定源(TextBlock、Border 等),并且当单元格第二次单击(尽管这可能会因属性而改变)。
Here's a not very elegant solution:
Only the X is there, but it's the same for the Y. The main problem is that a switch will be needed to determine the source (TextBlock, Border, etc.) and that this works when the cell is clicked the second time (although that might be changeable with properties).