如何从WPF中的DataGridCell中找到其所有者DataGrid和DataGridRow?
在 DataGrid 命令的事件处理程序中,我在 ExecutedRoulatedEventArgs 中获取 DataGridCell。但是,我不知道如何获取其关联的 DataGrid 和 DataGridRow。非常感谢您的帮助。
In an event handler for a Command for a DataGrid, I get DataGridCell in ExecutedRoutedEventArgs. However, I couldn't figure out how to get its associated DataGrid and DataGridRow. Your help is much appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可能想要设置某种
RelativeSource
绑定,可以通过{RelativeSource FindAncestor, AncestorType={x:Type 获取“父网格/行” DataGrid}}
,但你的问题让我思考......你可以:
使用反射:
使用
VisualTreeHelper
:You probably want to set some sort of
RelativeSource
binding that can get you the "parent grid/row" via a{RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}
, but your question got me thinking...You could:
Use Reflection:
Use the
VisualTreeHelper
:这是我认为完整的答案......
Here is what I think is a complete answer...
您可以做的一种方法是将一个或两个所需元素作为 CommandParameter 传递:
如果您需要两者,您可以添加一个多值转换器,将它们组合成一个
Tuple
(或保留它)作为对象[])然后在您的代码隐藏中您可以使用
e.Parameter
访问它One way you could do is by passing one or both of the needed elements in as a CommandParameter:
If you need both, you could add a multi-value converter that combines them into a
Tuple
(or leave it as an object[])Then in your code-behind you can access it by using
e.Parameter