I want to find the active node on which context menu was opened and a menu item was selected. I wrote the below code but it didnt work.
Flow of events is: node->rt click->context menu->first menu item selected->menu closes
private void XamContextMenu_ItemClicked(object sender, ItemClickedEventArgs e) { string tblName = null, dbName = null; if (e.Item.Header.ToString() == "Add to Group") { XamDataTreeNode currentNode = Utilities.GetAncestorFromType(e.Item.Parent as DependencyObject, typeof(XamDataTreeNode), true) as XamDataTreeNode; if (currentNode != null) { tblName = ((Tables)currentNode.Data).TableName.ToString(); XamDataTreeNode parentNode = currentNode.Manager.ParentNode; dbName = ((Databases)parentNode.Data).DBName.ToString(); DataSet ds = Program.GetColFromTable(tblName, dbName); if (outputRawData == null) outputRawData = new DataSet(); outputRawData.Tables.Add(ds.Tables[0]); ReRenderGrid(ds); } } }
HI,
The DataContext of the MenuItem should contain the XamDataTreeNodeDataContext.
I am attaching a sample application that should resolve your issue.
Im using a XamDataTree and a XamContextMenu instead of ContextMenu. I tried code from your example but XamDataTreeNodeDataContext xdtndc is null as xmi.DataContext is null.
private void XamMenuItem_Click(object sender, EventArgs e) { XamMenuItem xmi = sender as XamMenuItem; XamDataTreeNodeDataContext xdtndc = xmi.DataContext as XamDataTreeNodeDataContext;
}
<ig:XamDataTree x:Name="treeInstance" NodeLineVisibility="Hidden" Grid.Column="0" Margin="0,1,0,-1"> <ig:ContextMenuService.Manager> <ig:ContextMenuManager> <ig:ContextMenuManager.ContextMenu> <ig:XamContextMenu Name="TableNameMenu" Opening="XamContextMenu_Opening"> <ig:XamMenuItem Header="Update" Click="XamMenuItem_Click"></ig:XamMenuItem> <ig:XamMenuItem Header="Remove"></ig:XamMenuItem> </ig:XamContextMenu> </ig:ContextMenuManager.ContextMenu> </ig:ContextMenuManager> </ig:ContextMenuService.Manager>