如何获取wpf数据网格的点击事件?

时间:2022-01-27 19:35:26

I have a DataGrid with programmaticaly added rows (items) by adding an ItemSource. I do not want the user to edit the cells so I set IsReadOnly = true;. But I want them to click on cells and output which cell was clicked.

我有一个DataGrid与programmaticaly通过添加一个ItemSource添加行(项)。我不希望用户编辑单元格,所以我设置IsReadOnly = true;。但我希望他们点击单元格并输出单击的单元格。

I was searching for a Clicked event and was surprised there is no such event for a DataGrid. I want a behaviour like buttons where it doesn't matter if it's clicked by the mouse, space bar, enter or whatever else the system accepts as a "Click".

我正在寻找一个Clicked事件,并且很惊讶DataGrid没有这样的事件。我想要一个类似于按钮的行为,如果它被鼠标,空格键,输入或系统接受的任何其他任何“点击”所点击无关紧要。

So how can I achieve that?

那我怎么能实现呢?

1 个解决方案

#1


0  

Whatever is in the cell template (TextBlock, for instance) could be styled to handle the PreviewMouseUp event and pull the information you want from the control or bound object. The click event is specific to Button, TextBox and other controls that deal with mouse capture. Here is a link that discusses the dilemma you are facing: http://blog.scottlogic.com/2008/12/02/wpf-datagrid-detecting-clicked-cell-and-row.html. He provides examples on how to accomplish the task at hand.

无论单元格模板中是什么(例如TextBlock)都可以设置为处理PreviewMouseUp事件的样式,并从控件或绑定对象中提取所需的信息。 click事件特定于Button,TextBox和其他处理鼠标捕获的控件。这是一个链接,讨论您面临的困境:http://blog.scottlogic.com/2008/12/02/wpf-datagrid-detecting-clicked-cell-and-row.html。他提供了有关如何完成手头任务的示例。

#1


0  

Whatever is in the cell template (TextBlock, for instance) could be styled to handle the PreviewMouseUp event and pull the information you want from the control or bound object. The click event is specific to Button, TextBox and other controls that deal with mouse capture. Here is a link that discusses the dilemma you are facing: http://blog.scottlogic.com/2008/12/02/wpf-datagrid-detecting-clicked-cell-and-row.html. He provides examples on how to accomplish the task at hand.

无论单元格模板中是什么(例如TextBlock)都可以设置为处理PreviewMouseUp事件的样式,并从控件或绑定对象中提取所需的信息。 click事件特定于Button,TextBox和其他处理鼠标捕获的控件。这是一个链接,讨论您面临的困境:http://blog.scottlogic.com/2008/12/02/wpf-datagrid-detecting-clicked-cell-and-row.html。他提供了有关如何完成手头任务的示例。