我创建了一个代码来获取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"。存在显式转换,您是否错过了演员?
我做错了什么?