public XmlGridView() { InitializeComponent(); _xmlGridView.MouseDown += (sender, args) => { _contextMenuAtPos = args.Location; }; _xmlGridView.ContextMenu = new ContextMenu( new[] { new MenuItem("Refresh", (sender, evt) => load()), new MenuItem("Expand all", (sender, evt) => _xmlGridView.FullExpand()), new MenuItem("-"), new MenuItem("Copy", (sender, evt) => { Rectangle rect = new Rectangle(); GridCell cell = _xmlGridView.FindCellByPoint(_contextMenuAtPos, ref rect); cell.CopyToClipboard(); }), new MenuItem("Copy XPath", (sender, evt) => { Rectangle rect = new Rectangle(); GridCell cell = _xmlGridView.FindCellByPoint(_contextMenuAtPos, ref rect); XmlNode node; if (cell is XmlLabelCell) { node = ((XmlLabelCell)cell).Node; } else if (cell is XmlValueCell) { node = ((XmlValueCell)cell).Node; } else if (cell is XmlDeclarationCell) { node = ((XmlDeclarationCell)cell).Node; } else if (cell is XmlGroupCell) { node = ((XmlGroupCell)cell).Node; } else { MessageBox.Show("Selected cell is not a XML node. Can't copy XPath", "Error", MessageBoxButton.OK, MessageBoxImage.Error); return; } if (node != null) { var xpath = XPathBuilder.GetXPathToNode(node); Clipboard.SetText(xpath); } }), }); }