不能隐含转换父控件名称的类型

时间:2015-08-24 18:47:13

标签: c# wpf

我创建了一个代码来获取ContexMenu的父名称,特别是当用户点击MenuItem的{​​{1}}时发生此事:

ContextMenu

但在DataGrid -> ContextMenu (MenuItem 1 was clicked) -> function through code is started -> function return the name of the DataGrid. private void ClearTable_Click(object sender, RoutedEventArgs e) { // Try to cast the sender to a Control Control ctrl = sender as Control; if (ctrl != null) { // Get the control name string name = ctrl.Name; // Get parent control name Control parent = ctrl.Parent; string parentName = parent.Name; } } 我收到此错误:

  

不能隐含转换类型' System.Windows.DependencyObject" in" System.Windows.Controls.Control"。存在显式转换,您是否错过了演员?

我做错了什么?

0 个答案:

没有答案